Skip to content

fix(openai): capture refusals from streamed chat completions - #907

Open
pcbeingused333 wants to merge 2 commits into
open-telemetry:mainfrom
pcbeingused333:fix/openai-stream-refusal
Open

pcbeingused333 wants to merge 2 commits into
open-telemetry:mainfrom
pcbeingused333:fix/openai-stream-refusal

Conversation

@pcbeingused333

Copy link
Copy Markdown

Description

Streamed chat completions that end in a refusal are now recorded with the refusal text. The non-streaming path already captures message.refusal as a text part, but the streaming path ignored delta.refusal, so a streamed refusal produced an output message with no parts.

Type of change

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

How has this been tested?

  • New tests/test_chat_stream_refusal.py: a refusal streamed through ChatStreamWrapper and AsyncChatStreamWrapper is captured as one text part (fails on main).
  • test_choicebuffer_append_refusal in tests/test_choice_buffer.py.
  • tests/ of the package (without conformance): 528 passed, 13 skipped (main: 525 passed, 13 skipped).

Checklist

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

This change was made with Claude Code; the tests above were run on main and on this branch.

A refused chat completion carries its text in `refusal` instead of
`content`. The non-streaming path already records it as a text part, but
the streaming path ignored `delta.refusal`, so a streamed refusal produced
an output message with no parts. Buffer the refusal deltas per choice and
emit them as a text part, as the non-streaming path does.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner October 16, 2026 22:07
Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 22:07
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

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

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-10-17 03:25 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

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.

🟢 Approval recommended

The focused fix preserves existing capture behavior and includes regression coverage for both streaming wrappers, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Fixes missing refusal text in OpenAI streamed chat-completion telemetry, matching the existing non-streaming behavior.

Changes:

  • Buffers refusal deltas and emits them as a text part.
  • Adds buffer and sync/async streaming regression tests.
  • Records the fix in the changelog.
File Description
instrumentation/​opentelemetry-instrumentation-genai-openai/​tests/​test_choice_buffer.py Tests refusal accumulation.
instrumentation/​opentelemetry-instrumentation-genai-openai/​tests/​test_chat_stream_refusal.py Tests sync/async refusal capture.
instrumentation/​opentelemetry-instrumentation-genai-openai/​src/​opentelemetry/​instrumentation/​genai/​openai/​chat_wrappers.py Captures refusal deltas in output messages.
instrumentation/​opentelemetry-instrumentation-genai-openai/​src/​opentelemetry/​instrumentation/​genai/​openai/​chat_buffers.py Adds per-choice refusal buffering.
instrumentation/​opentelemetry-instrumentation-genai-openai/​.changelog/​907.fixed Documents the fix.

Review effort: Balanced

@pcbeingused333

Copy link
Copy Markdown
Author

/easycla

@pcbeingused333
pcbeingused333 force-pushed the fix/openai-stream-refusal branch from 3dbce2a to 05ee0eb Compare October 17, 2026 03:25

This branch has not been deployed

No deployments
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.

2 participants