Repository navigation
Fix async event fetches sending usage as an API parameter - #1996
Open
ajaykr0905 wants to merge 1 commit into
Open
ajaykr0905 wants to merge 1 commit into
ajaykr0905 wants to merge 1 commit into
Conversation
StripeClient.raw_request pops the internal `usage` kwarg and reports it
as request telemetry. raw_request_async did not: it always reported
["raw_request", "async"] and left `usage` in params. The async event
notification helpers pass usage=[...], so fetch_related_object_async()
and fetch_event_async() added usage[0]=... to the request. stripe-mock
rejects that query parameter on GET /v1/billing/meters/{id}.
Pop `usage` in raw_request_async when it is a list. The SDK's internal
callers always pass one. Any other value, such as a SetupIntent's
usage="off_session", is a real API parameter and stays in params, as it
did before. A plain copy of the sync pop would pass that string to
request_raw_async, which raises TypeError on usage + ["async"].
The async stub in test_v2_event.py expected the leaked
usage[0]=fetch_related_object query, so it now expects no query string.
Fixes stripe#1948
ajaykr0905
requested review from
zacchua-stripe
and removed request for
a team
November 16, 2026 14:54
jar-stripe
reviewed
Nov 16, 2026
| # append "async" to it. | ||
| usage = ["raw_request"] | ||
| if isinstance(params.get("usage"), list): | ||
| usage = params.pop("usage") |
Contributor
There was a problem hiding this comment.
this discards "raw_request" from the usage array, which we need to keep for logging usage through the raw_request mechanism.
Contributor
|
Hi @ajaykr0905 thanks for the PR! Can you please edit your PR description down to be more compact (I'd say no greater than a screen's height)? You can use e.g. #1972 as an example of what we typically do. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
StripeClient.raw_request()removes the internalusagekeyword fromparamsand reports it as request telemetry.raw_request_async()does not do this. It always reports["raw_request", "async"]and keeps the caller'susageinparams.The async event notification helpers pass
usage=[...]toraw_request_async(). Thusfetch_related_object_async()andfetch_event_async()addusage[0]=...to the query string. I ran the repro from the issue against stripe-mock. The async call for a/v1/billing/meters/...related object failed withInvalidRequestError(additional properties are not allowed). The sync call for the same object passed.After this change, the async helpers send no
usageparameter. Telemetry reports the tags that the caller passed, plus"async".What?
raw_request_async()removesusagefromparamswhen the value is a list. It passes that list to telemetry. All 630 internal call sites that passusagetoraw_request()orraw_request_async()pass a list.usagevalue stays inparams, as onmaster. For example,usage="off_session"is a real parameter of the SetupIntents API.usageraisesTypeError, becauserequest_raw_async()runsusage + ["async"].tests/test_v2_event.py, an async stub expected the leakedusage[0]=fetch_related_objectquery. The stub now expects no query string. A new async test checksfetch_event_async()andfetch_related_object_async().tests/test_raw_request.py, 3 async tests replace the commented-out async test. They check the default telemetry, a listusageand a stringusage..hark/changes/2026-11-16_ajaykr0905_async-event-fetch-usage.change.md(patch).I did not change the sync
raw_request(). It still removes everyusagevalue. Thus a syncusage="off_session"does not reach the API. That is older behavior, and this PR keeps it.Test Plan
I ran all checks on macOS arm64. stripe-mock 0.206.0 ran in Docker with the pinned
v2596spec and fixtures, as in CI.Revert check on Python 3.13.9, with
tests/test_raw_request.pyandtests/test_v2_event.py:stripe/_stripe_client.pymastermasterwith dae3d69TypeError, 41 passedtest_v2_event.py, frommasterOn
master, 3 tests fail on each anyio backend (asyncio and trio). The failing tests show the leaked query strings and the wrong telemetry. With the old stub, the stub test fails because the request now has no query string.Full suite with
pytest, asjust testruns it (-n autofrompyproject.toml):Static checks from
.github/actions/static-checks, on Python 3.13.9 with the pinned tool versions:mypy:no issues found in 2 source files.python -m flake8 --show-source stripe tests: no findings.ruff format . --check:1738 files already formatted.pyright1.1.336: 0 errors, 0 warnings.Other checks:
hark validate(v1.2.0, the release that CI installs):validated 632 changefiles.python -m buildandtwine checkon Python 3.10.19: both artifacts passed.The repro from the issue, against stripe-mock:
fetch_related_object_async()sentGET /v1/billing/meters/mtr_123?usage[0]=fetch_related_objectand failed withInvalidRequestError.GET /v1/billing/meters/mtr_123and returned aMeter. Telemetry reported["fetch_related_object", "async"].Unrecognized request URLforGET /v2/core/events/evt_1, for the sync and the async call. Forfetch_event_async(), I could only check the URL. After the fix, the URL has no query string.I did not run these:
justitself. I ran the commands that its recipes run.See Also
Fixes #1948
master. The existing async stub test failed, and a stringusageraisedTypeError.stripe/_stripe_client.py. I merged each shared file locally withgit merge-file, and git found no conflicts.Configuration
skip-changefile: This PR is not a user-facing change, so there's no changefile.