Skip to content

Record OpenAI refusals in gen_ai.output.messages - #2582

Merged
alexmojaki merged 1 commit into
pydantic:mainfrom
pcbeingused333:fix/openai-semconv-refusal
Oct 19, 2026
Merged

alexmojaki merged 1 commit into
pydantic:mainfrom
pcbeingused333:fix/openai-semconv-refusal

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

When a model refuses a request (for example with structured outputs), OpenAI leaves content empty and returns the text in message.refusal. convert_openai_response_to_semconv only read content (and tool calls), so a refused call was recorded with an output message that had no parts. Both streamed calls (through ChatCompletionStreamState, whose snapshot carries refusal) and non-streamed calls went through it.

The refusal is now added as a text part, the same way the OpenTelemetry GenAI OpenAI instrumentation records it.

Tests, in tests/otel_integrations/test_openai.py:

  • test_convert_openai_response_refusal: a non-streamed message with only refusal.
  • test_chat_completion_stream_state_refusal: a refusal streamed over two chunks through OpenaiChatCompletionStreamState.

Both fail on main (parts: []) and pass with this change. tests/otel_integrations/test_openai.py: 72 passed (70 on main). ruff format --check, ruff check and pyright on the changed module are clean.

This change was written with Claude Code; the tests and checks above were run before opening the PR.

View guided diff

When a model refuses, OpenAI leaves `content` empty and puts the text in
`message.refusal`. convert_openai_response_to_semconv only read `content`, so
both streamed and non-streamed refusals produced an output message with no
parts. The refusal is now recorded as a text part, as the OpenTelemetry GenAI
OpenAI instrumentation does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal/integrations/llm_providers
  openai.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Oct 19, 2026
@alexmojaki

Copy link
Copy Markdown
Contributor

Thanks!

Merged via the queue into pydantic:main with commit dc2ddff Oct 19, 2026
28 checks passed
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.

2 participants