Skip to content

fix(anthropic): stop leaked VCR httpx patch from failing live tests#38888

Open
Mason Daugherty (mdrxy) wants to merge 1 commit into
masterfrom
mdrxy/anthropic/vcr-httpx-patch-leak
Open

fix(anthropic): stop leaked VCR httpx patch from failing live tests#38888
Mason Daugherty (mdrxy) wants to merge 1 commit into
masterfrom
mdrxy/anthropic/vcr-httpx-patch-leak

Conversation

@mdrxy

@mdrxy Mason Daugherty (mdrxy) commented Jul 16, 2026

Copy link
Copy Markdown
Member

Scheduled Anthropic integration runs have been flaking with a confusing mix of anthropic.APIConnectionError: Connection error. and vcr.errors.CannotOverwriteExistingCassetteException on live (non-cassette) tests. The failures always cluster on a single pytest-xdist worker and reference one unrelated cassette (e.g. test_code_execution.yaml.gz), while the same tests pass on other workers.

The culprit is a vcrpy teardown bug. CassetteContextDecorator.__exit__ calls cassette._save() before it un-patches httpx; if the save raises (a dirty cassette whose serialization fails), the un-patch step is skipped and httpx.HTTPTransport.handle_request stays monkeypatched for the rest of that worker's session. Every later request — including live integration tests that should hit the real API — is then intercepted by the stale cassette and fails. I reproduced this locally: a cassette that errors on save leaves httpx patched, and mock.patch stacking lets that leaked wrapper resurface under otherwise-clean cassette tests.

Rather than depend on a vcrpy fix, this adds a small defensive guard in the Anthropic test suite: it captures the pristine httpx transport handlers at import time and, via an autouse fixture, restores them around every test that is not @pytest.mark.vcr (i.e. the live network tests). Cassette-backed tests are untouched, so VCR keeps managing its own patching; a leak from one test can no longer cascade into the live tests that follow.

Made by Open SWE

vcrpy skips its httpx un-patch step when saving a cassette raises during
teardown, leaving httpx.HTTPTransport patched for the rest of the worker
session. Later live integration tests then replay against the wrong
cassette and fail with CannotOverwriteExistingCassetteException /
APIConnectionError. Add an autouse fixture that restores pristine httpx
transports around non-VCR tests so a leak can't cascade.

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
@github-actions github-actions Bot added anthropic `langchain-anthropic` package issues & PRs fix For PRs that implement a fix integration PR made that is related to a provider partner package integration internal size: S 50-199 LOC labels Jul 16, 2026
@mdrxy
Mason Daugherty (mdrxy) marked this pull request as ready for review July 16, 2026 18:46

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Open SWE Review: No issues found

Open SWE reviewed this PR and found no potential bugs to report.

Open in WebView Open SWE trace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anthropic `langchain-anthropic` package issues & PRs fix For PRs that implement a fix integration PR made that is related to a provider partner package integration internal size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant