Skip to content

Conversation

@448523760
Copy link
Contributor

This pull request enhances the SSE (Server-Sent Events) processing logic in chat_completions.rs to improve how response metadata is handled, specifically by capturing and propagating the response_id and detailed token usage statistics from the OpenAI API responses. The changes ensure that this information is correctly parsed, stored, and included in the ResponseEvent::Completed messages, making downstream processing more robust and informative.

Improvements to response metadata handling:

  • Added a parse_openai_usage function to extract detailed token usage statistics—including input, output, cached, and reasoning tokens—from the OpenAI API response, and populate a TokenUsage struct.
  • Updated the SSE processing loop in process_chat_sse to capture and store the response_id and parsed token_usage from each incoming chunk, ensuring these fields are preserved across the session.

Enhancements to event emission:

  • Modified all ResponseEvent::Completed emissions to include the actual response_id and token_usage instead of defaulting to empty or None, improving traceability and observability for clients consuming these events.

Link Issue: #6834

@etraut-openai
Copy link
Collaborator

Thanks for the contribution. We've updated our contribution guidelines to clarify that we're currently accepting contributions for bugs and security fixes, but we're not generally accepting new features at this time. We need to make sure that all new features compose well with both existing and upcoming features and fit into our roadmap. If you would like to propose a new feature, please file or upvote an enhancement request in the issue tracker. We will generally prioritize new features based on community feedback.

This PR falls somewhere in the gray area between a feature and a bug fix, so it's something we might consider. Before I ask someone on the codex team to look at it, please fix the tests, which are failing currently.

@etraut-openai etraut-openai added the needs-response Additional information is requested label Nov 20, 2025
- Capture `token_usage` and `response_id` from SSE chunks in `process_chat_sse`.
- Add `parse_openai_usage` helper to map OpenAI usage fields tofeat: parse and return token usage in Chat Completions stream

- Capture `token_usage` and `response_id` from SSE chunks in `process_chat_sse`.
- Add `parse_openai_usage` helper to map OpenAI usage fields to the internal  struct.
- Ensure  is included in  when the stream finishes or receives `[DONE]`.
- Correctly map , , , and detailed token counts.
@448523760 448523760 force-pushed the chat_completions_token_usage branch from cf0e4ed to 44c91c7 Compare November 21, 2025 02:59
@etraut-openai etraut-openai removed the needs-response Additional information is requested label Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants