fix(openai): trace responses.retrieve so background responses get output and usage - #1833
Open
Kairick wants to merge 1 commit into
Open
fix(openai): trace responses.retrieve so background responses get output and usage#1833Kairick wants to merge 1 commit into
Kairick wants to merge 1 commit into
Conversation
Author
|
recheck |
…put and usage Responses.create and Responses.parse are instrumented, Responses.retrieve is not. Background responses (background=True) return immediately with status queued and no usage, and complete through retrieve() - either directly or via responses.stream(response_id=...), which delegates to retrieve(stream=True). The result is a generation in the trace that never receives its output, token counts or cost, which reads as a free call rather than a missing one. Instrument Responses.retrieve and AsyncResponses.retrieve. The response id is recorded as the generation input, since retrieve() carries no prompt of its own; model, output and usage come from the response, so both the streaming and non-streaming paths are handled by the existing extraction code.
Kairick
force-pushed
the
fix/trace-responses-retrieve
branch
from
August 24, 2026 09:08
e3b0671 to
9c417d3
Compare
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.
What does this PR do?
Instruments
Responses.retrieveandAsyncResponses.retrieve, so background responses get their output, token counts and cost.createandparseare instrumented,retrieveis not. Withbackground=Truethe create call returnsstatus="queued"with no usage, and the real result arrives throughretrieve()— either directly, or viaresponses.stream(response_id=...), which delegates toretrieve(stream=True). The generation therefore stays in the trace with an input and a model but no output, nousage_detailsand no cost, which reads as a free call rather than a missing one.The response id is recorded as the generation input, since
retrieve()carries no prompt of its own. Model, output and usage come from the response, so both the streaming and non-streaming paths are handled by the existing extraction code.A key-free reproduction of the routing is in the issue.
Open question: retrieving a response that was already traced at creation time (non-background usage) will now record its usage a second time. This PR implements the straightforward version — instrument every
retrieve— but restricting it tostream=True, or skipping usage when the response was notqueued, are both easy to switch to if you prefer one of those.Fixes #1834
Type of change
Verification
Both new tests fail on
mainand pass with the change.The 18 errors in the unit run come from
tests/unit/test_prompt.py, which needs Langfuse credentials in the environment. They reproduce unchanged onmain.Not run:
tests/e2eandtests/live_provider, which need a Langfuse server and provider keys. Per AGENTS.md this change is covered by exporter-local unit assertions, so no e2e or live-provider coverage was added.Checklist
code_review.md..env.templateif needed. (Not needed: no public API surface or env variable changes.)Greptile Summary
Instruments synchronous and asynchronous OpenAI Responses retrieval so completed background responses produce Langfuse generation telemetry.
Responses.retrieveandAsyncResponses.retrievefor tracing.Confidence Score: 5/5
The PR appears safe to merge, with no unacknowledged blocking or independently actionable issues identified.
The new retrieval registrations use the existing synchronous, asynchronous, and Responses API extraction paths, and the added tests cover the primary non-streaming behavior.
Reviews (1): Last reviewed commit: "fix(openai): trace responses.retrieve so..." | Re-trigger Greptile