Skip to content

fix(openai): fix tracing exception in model response getting method when tracing is disabled #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

upphiminn
Copy link

This pull request addresses a possible issue in the OpenAIResponsesModel where an exception could occur during getting a response when tracing is disabled. When tracing is disabled there is no parent trace so withResponseSpan fails with not finding a context trace for the span it attempts to create. This leads to the whole getResponse failing. Fix found below copies the same style of span creation only when tracing is enabled as seen in getStreamedResponse method.

@upphiminn upphiminn marked this pull request as ready for review July 17, 2025 15:55
@seratch seratch added package:agents-openai bug Something isn't working labels Jul 17, 2025
Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

This looks good to me; @dkundel-openai any concern?

@dkundel-openai
Copy link
Collaborator

This seems odd because if tracing is disabled there should still be a NoopTrace that this should get attached to?

@dkundel-openai
Copy link
Collaborator

The main thing is that not using withResponseSpan() means that we are not creating a new entry in the Async Context Store which could create race condition issues so we should avoid it wherever possible

@seratch
Copy link
Member

seratch commented Jul 25, 2025

@dkundel-openai thanks for the good point.
@upphiminn can you share what actually happens in this scenario? > When tracing is disabled there is no parent trace so withResponseSpan fails with not finding a context trace for the span it attempts to create.

If we figure the root cause out, the fix can be much simpler than rewriting a bunch of code.

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

to avoid confusion, let me revert the approval for now

@upphiminn
Copy link
Author

upphiminn commented Jul 28, 2025

Hello 👋🏻 @seratch @dkundel-openai, thank you for the reply. The issue can be verified by running the unit test that was added on the original code. When the tracing is set to disabled the code throws a 'No existing trace found' exception. I don't know the internal details of the tracing code, but it seems that even in the tests (for example the one at line 15 in the unit test file modified) there is an assumption that a trace exists. In practice, while looking into it, I didn't find the code path will create a Noop trace by the time it gets there.

The overall usage context was using the (Azure OpenAI Service) Github Models API, which doesn't seem to work with the Agents JS SDK as far as I tried, but does work with the Agents Python one. Due to this API not supporting tracing it must be disabled but that seems to fail somehow (although I tried in many ways to disable tracing, via env var flag, via RunConfig, via globalTraceProvider directly or via ModelRequest tracing property, none seem to had the desired effect, and this seemed like one of the causes of failure – I also might have tried too hard to disable it 😄)

@upphiminn
Copy link
Author

@seratch @dkundel-openai Here is also a sample stack trace when attempting to call the API:

{"message":"No existing trace found","name":"Error","stack":"Error: No existing trace found\n    at withNewSpanContext (/Users/xxx-redacted-xxx/node_modules/@openai/agents-openai/node_modules/@openai/agents-core/dist/tracing/context.js:141:15)\n    at <anonymous> (/Users/xxx-redacted-xxx/node_modules/@openai/agents-openai/node_modules/@openai/agents-core/dist/tracing/createSpans.js:6:16)\n    at OpenAIResponsesModel.getResponse (/Users/xxx-redacted-xxx/node_modules/@openai/agents-openai/dist/openaiResponsesModel.js:725:32)\n    at <anonymous> (/Users/xxx-redacted-xxx/node_modules/@openai/agents/node_modules/@openai/agents-core/dist/run.js:151:63)" }

Overall the code looks like this:

  // config
  const customClient = new OpenAI({ baseURL: _config.github.modelEndpoint, apiKey: _config.github.token })
  setTracingDisabled(true)
  setDefaultOpenAIClient(customClient)

   // usage
   const agent = new Agent({
    name: 'Assistant',
    instructions: 'You are a helpful assistant',
  })
  const result = await run(agent, 'Write a haiku about recursion in programming.', {})

The version used are:

  "@openai/agents": "0.0.14",
  "openai": "5.10.2"

Copy link
Contributor

github-actions bot commented Aug 9, 2025

This PR is stale because it has been open for 10 days with no activity.

@github-actions github-actions bot added the stale label Aug 9, 2025
@Nirator78
Copy link

Any news for this bug ?

@github-actions github-actions bot removed the stale label Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package:agents-openai
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants