Skip to content

Conversation

@punitmahes
Copy link
Contributor

@punitmahes punitmahes commented Dec 9, 2025

Description

This PR fixes an AttributeError that occurs when using the OpenAI client with with_raw_response. In this scenario, the result object returned is a LegacyAPIResponse which does not directly expose attributes like .model or .choices, causing the instrumentation to crash when attempting to set span attributes or record metrics.

Changes implemented:

Response Parsing: Added logic to detect if the result has a .parse() method (indicative of LegacyAPIResponse). If present, it calls .parse() to retrieve the actual response object containing the necessary attributes (model, choices, etc.).

Safe Attribute Access: Updated _set_response_attributes to use getattr for safer access to .model to prevent future AttributeErrors.

Fixes #4002

Type of change

Please delete options that are not relevant.

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

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
I have added new unit tests specifically for the with_raw_response flow for both synchronous and asynchronous clients, using VCR cassettes to mock the OpenAI API interaction.

  • test_chat_completion_with_raw_repsonse (Added to tests/test_chat_completions.py) - Verifies that synchronous chat completions with raw response are correctly instrumented without errors.
  • test_async_chat_completion_with_raw_repsonse (Added to tests/test_async_chat_completions.py) - Verifies that asynchronous chat completions with raw response are correctly instrumented without errors.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@punitmahes punitmahes requested a review from a team as a code owner December 9, 2025 18:08
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 9, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: punitmahes / name: Punit Maheshwari (a012d67)
  • ✅ login: xrmx / name: Riccardo Magliocchetti (55b16eb, aa48150)

Copy link
Member

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

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

Thanks!

@punitmahes punitmahes changed the title [WIP] fix(request-model): Add check for the model attr in request object in openaiv2 fix(request-model): Add check for the model attr in request object in openaiv2 Dec 9, 2025
@xrmx
Copy link
Contributor

xrmx commented Dec 10, 2025

@punitmahes Thanks for the PR, looks like some of the commits are done under a different name. Please fix the git history so that all commits are covered by the CLA

@punitmahes
Copy link
Contributor Author

@xrmx Done

@punitmahes punitmahes requested a review from lmolkova December 10, 2025 17:46
Copy link
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

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

Please add an entry inside the instrumentation CHANGELOG

@xrmx xrmx moved this to Approved PRs that need fixes in @xrmx's Python PR digest Dec 11, 2025
@xrmx xrmx enabled auto-merge (squash) December 11, 2025 16:01
@xrmx xrmx disabled auto-merge December 11, 2025 16:02
@xrmx xrmx enabled auto-merge (squash) December 11, 2025 16:02
@xrmx xrmx merged commit 46a0d1d into open-telemetry:main Dec 11, 2025
658 checks passed
@github-project-automation github-project-automation bot moved this from Approved PRs that need fixes to Done in @xrmx's Python PR digest Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-openai-v2: crash with with_raw_response: 'LegacyAPIResponse' object has no attribute 'model'

7 participants