-
Notifications
You must be signed in to change notification settings - Fork 2
Handle incomplete/failed streaming responses and expose usage in metadata. #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
homin-bt
wants to merge
4
commits into
main
Choose a base branch
from
issue115
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e2f5cc
chore(examples): migrate openai example to Responses API with status …
homin-bt 715e38b
feat(openai): capture status/usage in metadata for incomplete/failed …
homin-bt e867cfa
chore: remove non-existent make command
homin-bt 3521459
feat(openai): capture incomplete_details/error in metadata; add integ…
homin-bt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,3 +65,6 @@ examples/internal/email-evals/email-evals | |
| /dist | ||
| # Added by goreleaser init: | ||
| dist/ | ||
|
|
||
| # emacs | ||
| *~ | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,9 +130,8 @@ func (rt *responsesTracer) parseStreamingResponse(span trace.Span, body io.Reade | |
| } | ||
|
|
||
| if msgType, ok := envelope["type"].(string); ok { | ||
| // the response.completed message has everything, so just parse that. Should we | ||
| // parse the other messages too? | ||
| if msgType == "response.completed" { | ||
| switch msgType { | ||
| case "response.completed", "response.failed", "response.incomplete": | ||
| if msg, ok := envelope["response"].(map[string]any); ok { | ||
| // For streaming responses, copy extra fields from the envelope | ||
| // that might be present in the outer wrapper | ||
|
|
@@ -181,6 +180,7 @@ func (rt *responsesTracer) handleResponseCompletedMessage(span trace.Span, rawMs | |
| metadataFields := []string{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that we are collecting |
||
| "id", | ||
| "object", | ||
| "status", | ||
| "system_fingerprint", | ||
| "completion_tokens", | ||
| "created", | ||
|
|
@@ -193,6 +193,9 @@ func (rt *responsesTracer) handleResponseCompletedMessage(span trace.Span, rawMs | |
| "content_filter_results", | ||
| "reasoning", | ||
| "text", | ||
| "usage", | ||
| "incomplete_details", | ||
| "error", | ||
| } | ||
|
|
||
| for _, field := range metadataFields { | ||
|
|
||
164 changes: 164 additions & 0 deletions
164
trace/contrib/openai/testdata/cassettes/TestOpenAIResponsesCompletedUsage.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| --- | ||
| version: 2 | ||
| interactions: | ||
| - id: 0 | ||
| request: | ||
| proto: HTTP/1.1 | ||
| proto_major: 1 | ||
| proto_minor: 1 | ||
| content_length: 64 | ||
| transfer_encoding: [] | ||
| trailer: {} | ||
| host: "" | ||
| remote_addr: "" | ||
| request_uri: "" | ||
| body: '{"input":"What is the capital of France?","model":"gpt-4o-mini"}' | ||
| form: {} | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Content-Type: | ||
| - application/json | ||
| User-Agent: | ||
| - OpenAI/Go 1.12.0 | ||
| X-Stainless-Arch: | ||
| - arm64 | ||
| X-Stainless-Lang: | ||
| - go | ||
| X-Stainless-Os: | ||
| - MacOS | ||
| X-Stainless-Package-Version: | ||
| - 1.12.0 | ||
| X-Stainless-Retry-Count: | ||
| - "0" | ||
| X-Stainless-Runtime: | ||
| - go | ||
| X-Stainless-Runtime-Version: | ||
| - go1.26.2 | ||
| url: https://api.openai.com/v1/responses | ||
| method: POST | ||
| response: | ||
| proto: HTTP/2.0 | ||
| proto_major: 2 | ||
| proto_minor: 0 | ||
| transfer_encoding: [] | ||
| trailer: {} | ||
| content_length: -1 | ||
| uncompressed: true | ||
| body: |- | ||
| { | ||
| "id": "resp_00297490fcbc5dcc0069f0d13e07b0819ba4cb70853f95cc0e", | ||
| "object": "response", | ||
| "created_at": 1777389886, | ||
| "status": "completed", | ||
| "background": false, | ||
| "billing": { | ||
| "payer": "developer" | ||
| }, | ||
| "completed_at": 1777389887, | ||
| "error": null, | ||
| "frequency_penalty": 0.0, | ||
| "incomplete_details": null, | ||
| "instructions": null, | ||
| "max_output_tokens": null, | ||
| "max_tool_calls": null, | ||
| "model": "gpt-4o-mini-2024-07-18", | ||
| "moderation": null, | ||
| "output": [ | ||
| { | ||
| "id": "msg_00297490fcbc5dcc0069f0d13f0a20819bb1002795595779e0", | ||
| "type": "message", | ||
| "status": "completed", | ||
| "content": [ | ||
| { | ||
| "type": "output_text", | ||
| "annotations": [], | ||
| "logprobs": [], | ||
| "text": "The capital of France is Paris." | ||
| } | ||
| ], | ||
| "role": "assistant" | ||
| } | ||
| ], | ||
| "parallel_tool_calls": true, | ||
| "presence_penalty": 0.0, | ||
| "previous_response_id": null, | ||
| "prompt_cache_key": null, | ||
| "prompt_cache_retention": "in_memory", | ||
| "reasoning": { | ||
| "effort": null, | ||
| "summary": null | ||
| }, | ||
| "safety_identifier": null, | ||
| "service_tier": "default", | ||
| "store": true, | ||
| "temperature": 1.0, | ||
| "text": { | ||
| "format": { | ||
| "type": "text" | ||
| }, | ||
| "verbosity": "medium" | ||
| }, | ||
| "tool_choice": "auto", | ||
| "tools": [], | ||
| "top_logprobs": 0, | ||
| "top_p": 1.0, | ||
| "truncation": "disabled", | ||
| "usage": { | ||
| "input_tokens": 14, | ||
| "input_tokens_details": { | ||
| "cached_tokens": 0 | ||
| }, | ||
| "output_tokens": 8, | ||
| "output_tokens_details": { | ||
| "reasoning_tokens": 0 | ||
| }, | ||
| "total_tokens": 22 | ||
| }, | ||
| "user": null, | ||
| "metadata": {} | ||
| } | ||
| headers: | ||
| Alt-Svc: | ||
| - h3=":443"; ma=86400 | ||
| Cf-Cache-Status: | ||
| - DYNAMIC | ||
| Cf-Ray: | ||
| - 9f3713632b7b420b-EWR | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Tue, 28 Apr 2026 15:24:47 GMT | ||
| Openai-Organization: | ||
| - braintrust-data | ||
| Openai-Processing-Ms: | ||
| - "1247" | ||
| Openai-Project: | ||
| - proj_vsCSXafhhByzWOThMrJcZiw9 | ||
| Openai-Version: | ||
| - "2020-10-01" | ||
| Server: | ||
| - cloudflare | ||
| Set-Cookie: | ||
| - __cf_bm=YyPcSpHIhDzxiZr_PPUz5BpglZaiN47o3bMWhvYrpQs-1777389885.94245-1.0.1.1-4pWcI.VMIhN1F7QL39HKPE2Vg2TBw7NY0KIK3C9oPVT8NPDyyLntvKFNnKngMo1zyBUN73JKAALmHagsaNm6ZNX5En.2r.Ce5xktre0looqN_4t5T3rQnqs5M2ZKI78t; HttpOnly; Secure; Path=/; Domain=api.openai.com; Expires=Tue, 28 Apr 2026 15:54:47 GMT | ||
| Strict-Transport-Security: | ||
| - max-age=31536000; includeSubDomains; preload | ||
| X-Content-Type-Options: | ||
| - nosniff | ||
| X-Ratelimit-Limit-Requests: | ||
| - "30000" | ||
| X-Ratelimit-Limit-Tokens: | ||
| - "150000000" | ||
| X-Ratelimit-Remaining-Requests: | ||
| - "29999" | ||
| X-Ratelimit-Remaining-Tokens: | ||
| - "149999967" | ||
| X-Ratelimit-Reset-Requests: | ||
| - 2ms | ||
| X-Ratelimit-Reset-Tokens: | ||
| - 0s | ||
| X-Request-Id: | ||
| - req_388462f6d6934e98b2adb25f5adaee39 | ||
| status: 200 OK | ||
| code: 200 | ||
| duration: 1.61271275s |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the
response.failedcase, can we capture the error somehow?