Skip to content

fix(openai): Record tool calls from streamed Chat Completions output - #8058

Open
ericapisani wants to merge 2 commits into
ep/py-2004-openai-integrationfrom
ep/openai-chat-stream-tool-calls
Open

ericapisani wants to merge 2 commits into
ep/py-2004-openai-integrationfrom
ep/openai-chat-stream-tool-calls

Conversation

@ericapisani

Copy link
Copy Markdown
Member

Description

Stacked on #8057. Streamed Chat Completions spans only collected delta.content, so tool call deltas were dropped and gen_ai.response.tool_calls was never set. A stream that only produced tool calls also recorded an empty string as gen_ai.response.text.

Accumulate tool call deltas per choice and tool call index, keeping arguments as the raw string, and set them as tool calls behind the same data collection / PII gate as the response text. Skip the response text when no content was streamed.

Issues

  • refs: PY-3004

Streamed Chat Completions spans only collected delta.content, so tool
call deltas were dropped and gen_ai.response.tool_calls was never set.
A stream that only produced tool calls also recorded an empty string as
gen_ai.response.text.

Accumulate tool call deltas per choice and tool call index, keeping
arguments as the raw string, and set them as tool calls behind the same
data collection / PII gate as the response text. Skip the response text
when no content was streamed.

Refs PY-3004
@linear-code

linear-code Bot commented Oct 19, 2026

Copy link
Copy Markdown

PY-3004

@ericapisani

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fe498b2. Configure here.

Comment thread sentry_sdk/integrations/openai.py
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 138594 passed | ⏭️ 8620 skipped | Total: 147214 | Pass Rate: 94.14% | Execution Time: 469m 16s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +6272
Passed Tests 📈 +4928
Failed Tests —
Skipped Tests 📈 +1344

All tests are passing successfully.

✅ Patch coverage is 100.00% (36 of 36 changed executable lines covered; target 80%).
Project statement coverage is 90.28% (up 0.03 percentage points from base (358b632) to head (eb099b5)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/integrations/openai.py 100.00% 36/36 covered; partial branches: 1048, 1090, 1182
Coverage diff
@@            Coverage Diff             @@
##        master     #8058       +/-##
==========================================
+ Coverage    90.25%    90.28%    +0.03%
==========================================
  Files          202       202         —
  Tracked lines     26660     26709       +49
  Branches      9926      9982       +56
==========================================
+ Hits         24062     24113       +51
+ Misses        2598      2596        -2
- Partials      1510      1513        +3

Generated by Coverage Action

@ericapisani
ericapisani added this pull request to stack #8065 October 19, 2026 14:08
With n > 1, OpenAI streams one choice per chunk, but the stream
iterators keyed text and tool call deltas by the choice's position
within the chunk. Every choice landed on position 0, so tool calls from
different choices were merged into one corrupted entry and the response
texts were concatenated.

Key the text buffer and tool call deltas by choice.index instead, and
order the recorded tool calls by choice and tool call index rather than
by when they were first seen. Update the single-response streaming
fixtures, which used increasing choice indices for fragments of the same
response.

Refs PY-3004
@ericapisani
ericapisani marked this pull request as ready for review October 19, 2026 14:52
@ericapisani
ericapisani requested a review from a team as a code owner October 19, 2026 14:52

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant