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
@448523760 448523760 force-pushed the chat_completions_token_usage branch from cf0e4ed to 44c91c7 Compare November 21, 2025 02:59
@etraut-openai etraut-openai added chat-endpoint Bugs or PRs related to the chat/completions endpoint (wire API) and removed needs-response Additional information is requested labels Nov 21, 2025
@448523760 448523760 force-pushed the chat_completions_token_usage branch from 44c91c7 to 8f6926a Compare November 26, 2025 14:20
- 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.
@448523760 448523760 force-pushed the chat_completions_token_usage branch from 8f6926a to c5be851 Compare November 26, 2025 14:25
@448523760
Copy link
Contributor Author

rebased on the latest refactor

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

Labels

chat-endpoint Bugs or PRs related to the chat/completions endpoint (wire API)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants