Skip to content

Populate gen_ai.response.model from responses API when the response body includes the served model header (OpenAI) - #355

Merged
lmolkova merged 6 commits into
open-telemetry:mainfrom
rads-1996:respect-rapi-headers-openai
Aug 6, 2026
Merged

Populate gen_ai.response.model from responses API when the response body includes the served model header (OpenAI)#355
lmolkova merged 6 commits into
open-telemetry:mainfrom
rads-1996:respect-rapi-headers-openai

Conversation

@rads-1996

@rads-1996 rads-1996 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes one part of # (348)

Added a guard to the response header value in the langchain implementation to ensure we don't accept empty string values.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.

  • uv run tox -e py312-test-instrumentation-genai-openai -- -q
  • uv run tox -e py312-test-instrumentation-genai-openai-conformance -- -q
  • uv run --python 3.12 tox -e lint-instrumentation-genai-openai
  • uv run tox -e py312-test-instrumentation-genai-langchain -- -q
  • uv run tox -e py312-test-instrumentation-genai-langchain-conformance -- -q
  • uv run --python 3.12 tox -e lint-instrumentation-genai-langchain

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

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

@rads-1996
rads-1996 force-pushed the respect-rapi-headers-openai branch from 14bdc52 to d9e4c67 Compare August 4, 2026 17:02
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 4, 2026

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-08-06 02:21 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@rads-1996
rads-1996 marked this pull request as ready for review August 4, 2026 20:28
@rads-1996
rads-1996 requested a review from a team as a code owner August 4, 2026 20:28
Copilot AI lite review requested due to automatic review settings August 4, 2026 20:28

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

This PR improves model-name attribution by preferring the served model reported via the x-ms-served-model HTTP response header (notably for OpenAI Responses API / Azure scenarios), and hardens LangChain’s header handling to avoid overwriting a real model name with an empty/whitespace header value.

Changes:

  • Add a get_served_model() helper and use it across OpenAI response extraction paths to populate gen_ai.response.model from x-ms-served-model when available.
  • Add unit tests for get_served_model() and adjust LangChain callback handling + test coverage to ignore empty/whitespace served-model header values.
  • Add changelog fragments documenting the user-visible behavior changes.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
instrumentation/opentelemetry-instrumentation-genai-openai/tests/test_chat_completions.py Adds unit tests covering get_served_model() behavior across header shapes and edge cases.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/utils.py Introduces SUPPORTED_RAPI_RESPONSE_HEADERS and get_served_model() helper for extracting the served model name.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/response_extractors.py Prefers served-model header over response.model when setting invocation response attributes.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/patch.py Uses served-model header in chat/embeddings response-property extraction.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/_raw_response.py Captures served-model header early for raw streaming responses to populate invocation model name.
instrumentation/opentelemetry-instrumentation-genai-openai/.changelog/355.added Documents the OpenAI served-model header behavior change for the OpenAI instrumentation package.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/test_callback_handler.py Updates test expectations to ensure whitespace header values do not overwrite an existing model name.
instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/callback_handler.py Tightens served-model header value guard (isinstance(value, str) + value.strip()).
instrumentation/opentelemetry-instrumentation-genai-langchain/.changelog/355.fixed Notes the fix preventing empty model-name overwrite in LangChain instrumentation.

@eternalcuriouslearner eternalcuriouslearner 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.

LGTM!! Just a small doubt, should we add a check to the conformance tests or do we have it there?

@rads-1996

Copy link
Copy Markdown
Contributor Author

LGTM!! Just a small doubt, should we add a check to the conformance tests or do we have it there?

I am not sure if adding conformance test for this scenario would make sense, we would not be able distinguish if the response model was populated though the served model or the deployment name. WDYT?

@rads-1996
rads-1996 force-pushed the respect-rapi-headers-openai branch from 8e0ae3a to 7b713d7 Compare August 5, 2026 21:26
@rads-1996

Copy link
Copy Markdown
Contributor Author

@eternalcuriouslearner The PR is ready for another review. Thanks a lot for the previous feedback.

@lmolkova
lmolkova added this pull request to the merge queue Aug 6, 2026
Merged via the queue into open-telemetry:main with commit cb5c2a3 Aug 6, 2026
103 checks passed
@rads-1996
rads-1996 deleted the respect-rapi-headers-openai branch August 7, 2026 16:51
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.

4 participants