Skip to content

fix(openai): expose headers on streaming with_raw_response wrapper#147

Open
YuxiangJiangCT wants to merge 1 commit into
open-telemetry:mainfrom
YuxiangJiangCT:fix/openai-chat-stream-headers
Open

fix(openai): expose headers on streaming with_raw_response wrapper#147
YuxiangJiangCT wants to merge 1 commit into
open-telemetry:mainfrom
YuxiangJiangCT:fix/openai-chat-stream-headers

Conversation

@YuxiangJiangCT

@YuxiangJiangCT YuxiangJiangCT commented Jun 16, 2026

Copy link
Copy Markdown

Description

chat.completions.with_raw_response.create(stream=True) returns a
ChatStreamWrapper, but accessing .headers on it raised AttributeError.
parse() drops the LegacyAPIResponse the headers come from, and the wrapper
didn't keep a reference. The Responses API wrapper already exposes headers
via .response; the chat path just never did.

This captures the headers before parse() and exposes them through a headers
property on the sync and async chat stream wrappers.

Fixes #46

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

Added regression tests for the sync and async paths
(test_chat_completion_with_raw_response_streaming_exposes_headers) asserting
raw_response.headers is accessible and that streaming still works. They fail
on main with AttributeError and pass with the fix.

  • uv run tox -e py312-test-instrumentation-genai-openai-latest — 161 passed
  • uv run tox -e typecheck — clean
  • ruff — clean

Checklist

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

@YuxiangJiangCT YuxiangJiangCT requested a review from a team as a code owner June 16, 2026 22:14
Copilot AI review requested due to automatic review settings June 16, 2026 22:14
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 16, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: YuxiangJiangCT / name: Yuxiang (Ryan) Jiang (3801e7e)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes a regression where with_raw_response + stream=True chat completions lost access to the original response headers after parsing, by carrying headers through the stream wrapper.

Changes:

  • Capture raw response headers before parse() discards the legacy response object (sync + async paths).
  • Extend chat stream wrappers to expose raw_response.headers.
  • Add sync/async regression tests asserting headers are accessible and streaming still works.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
instrumentation/opentelemetry-instrumentation-genai-openai/tests/test_chat_completions.py Adds sync regression test verifying .headers is exposed on streaming raw responses.
instrumentation/opentelemetry-instrumentation-genai-openai/tests/test_async_chat_completions.py Adds async regression test verifying .headers is exposed on streaming raw responses.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/patch.py Captures headers pre-parse and passes them into stream wrappers for sync/async instrumentation.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/chat_wrappers.py Adds headers property and plumbs headers through ChatStreamWrapper and AsyncChatStreamWrapper.

Comment on lines +1399 to +1400
@pytest.mark.asyncio()
async def test_chat_completion_with_raw_response_streaming_exposes_headers(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept @pytest.mark.asyncio() with parens to match the existing async tests in this file - changing only this one would be inconsistent. Happy to switch all of them in a separate cleanup if preferred.

Accessing .headers on the wrapper returned by
chat.completions.with_raw_response.create(stream=True) raised
AttributeError. parse() discards the LegacyAPIResponse that carries the
headers, and ChatStreamWrapper kept no reference to them. The responses
path already handles this, chat never did.

Capture the headers before parse() and expose them via a property on the
sync and async chat stream wrappers, with tests for both.

Fixes open-telemetry#46
@YuxiangJiangCT YuxiangJiangCT force-pushed the fix/openai-chat-stream-headers branch from 3801e7e to d7ccaee Compare June 16, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

openai-v2: StreamWrapper.headers fails for with_raw_response streaming even after #4184 fix

2 participants