Skip to content

fix(responses): preserve DeepSeek reasoning across tool turns - #623

Merged
SantiagoDePolonia merged 4 commits into
mainfrom
fix/codex2
Jul 31, 2026
Merged

fix(responses): preserve DeepSeek reasoning across tool turns#623
SantiagoDePolonia merged 4 commits into
mainfrom
fix/codex2

Conversation

@SantiagoDePolonia

@SantiagoDePolonia SantiagoDePolonia commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • preserve raw reasoning content when converting Chat Completions output into Responses API events and items
  • replay reasoning content only where providers require it for assistant tool-call messages, including DeepSeek
  • normalize developer messages, usage fields, output indexes, and late reasoning deltas for OpenAI-compatible behavior
  • keep the streaming conversion hot path within the existing allocation and byte thresholds

Validation

  • go test ./...
  • repository pre-commit suite: formatting, race tests, tidy check, performance guard, fix-check, and lint

Summary by CodeRabbit

  • New Features

    • Added support for displaying streamed reasoning content in Responses API outputs.
    • Reasoning now appears in the correct order before assistant messages and tool calls.
    • Reasoning is preserved when assistants invoke tools, improving continuity across tool-call workflows.
    • Developer messages are normalized for broader compatibility.
  • Bug Fixes

    • Improved handling of unsupported and malformed input and usage data.
    • Prevented late reasoning updates from disrupting response ordering.
    • Standardized token usage details in converted responses.

Copilot AI review requested due to automatic review settings July 31, 2026 13:45

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds reasoning replay for tool-call inputs, reasoning output items for non-streaming responses, and reasoning delta handling for streaming Responses conversion. It also normalizes usage fields and adds coverage for ordering, suppression, and malformed usage.

Changes

Reasoning content support

Layer / File(s) Summary
Input reasoning replay and message normalization
internal/providers/responses_input.go, internal/providers/responses_adapter_test.go, internal/providers/deepseek/deepseek_test.go
Reasoning items are extracted and attached to assistant tool-call messages as reasoning_content. Standalone reasoning is omitted. Developer roles become system. Tests cover message merging and DeepSeek request translation.
Reasoning output items
internal/providers/responses_output_state.go, internal/providers/responses_output.go, internal/providers/responses_adapter_test.go
Reasoning output items receive IDs, reasoning text, summary data, delta events, and completion events. Non-streaming output places reasoning before assistant content.
Streaming conversion and usage mapping
internal/providers/responses_converter.go
Streaming conversion reserves reasoning output indexes, emits deltas, completes reasoning before assistant or tool-call output, suppresses late reasoning, and maps valid usage fields to Responses fields.
Streaming regression coverage
internal/providers/responses_converter_test.go, tests/contract/testdata/golden/groq/responses_stream.golden.json
Tests cover reasoning order, late reasoning, tool-call transitions, usage details, normalized field names, malformed usage omission, and the updated golden response.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ModelStream
  participant ResponsesStreamConverter
  participant ResponsesOutputEventState
  ModelStream->>ResponsesStreamConverter: send reasoning_content delta
  ResponsesStreamConverter->>ResponsesOutputEventState: reserve and append reasoning
  ResponsesOutputEventState-->>ResponsesStreamConverter: emit reasoning delta event
  ModelStream->>ResponsesStreamConverter: send assistant or tool-call delta
  ResponsesStreamConverter->>ResponsesOutputEventState: complete reasoning
  ResponsesStreamConverter->>ResponsesOutputEventState: start later output item
Loading

Possibly related PRs

Suggested reviewers: copilot

Poem

A rabbit sends reasoning through the stream,
Then places tool calls in order.
Index zero holds the thought,
Usage fields take their proper names.
Tests guard every transition.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: preserving DeepSeek reasoning across tool-call turns.
Description check ✅ Passed The description explains the changes, rationale, affected behavior, and validation steps, although it uses Summary instead of the template's Description heading.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/codex2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

codecov-commenter commented Jul 31, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 80.80357% with 43 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/providers/responses_input.go 60.97% 24 Missing and 8 partials ⚠️
internal/providers/responses_converter.go 85.48% 5 Missing and 4 partials ⚠️
internal/providers/responses_output.go 90.90% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/providers/responses_converter.go`:
- Around line 488-514: Update chatUsageToResponsesUsage to remove the
bytes.Contains checks and represent the three required token counts as pointer
fields in the local JSON-unmarshal struct. Unmarshal once, return false when any
pointer is nil or unmarshalling fails, and dereference the validated pointers
when constructing responsesStreamUsage; preserve the existing omission behavior
for malformed usage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 87c6d21e-82dc-4ba0-9dc0-7b783c8a6a3b

📥 Commits

Reviewing files that changed from the base of the PR and between 61493f4 and 0ee3a91.

📒 Files selected for processing (7)
  • internal/providers/deepseek/deepseek_test.go
  • internal/providers/responses_adapter_test.go
  • internal/providers/responses_converter.go
  • internal/providers/responses_converter_test.go
  • internal/providers/responses_input.go
  • internal/providers/responses_output.go
  • internal/providers/responses_output_state.go

Comment thread internal/providers/responses_converter.go
@greptile-apps

greptile-apps Bot commented Jul 31, 2026

Copy link
Copy Markdown

Confidence Score: 3/5

T-Rex T-Rex Logs

What T-Rex did

  • T-Rex produced a finding-proof for a posted P1 finding and linked it to the corresponding review comment.
  • T-Rex produced a second finding-proof for another posted P1 finding and linked it to its review comment.
  • T-Rex performed a general contract validation for an index-collision scenario, capturing the before and after data and listing the observed events (reasoning, function_call, and assistant message outputs).

View all artifacts

T-Rex Ran code and verified through T-Rex

Comments Outside Diff (2)

  1. internal/providers/responses_converter.go, line 146-155 (link)

    P1 Started tool call and later assistant message share Responses output index

    When a stream emits reasoning, starts a tool call, and then emits assistant text, reserveAssistantOutput keeps the assistant at the post-reasoning index while only shifting tool calls that have not started. The already-started function call therefore remains at output index 1 and the later assistant message is also emitted at output index 1. Responses clients receive two distinct output items with the same output_index, breaking ordered output-item processing. Allocate a distinct assistant index after started tool calls, or otherwise prevent this event ordering from producing colliding items.

    Context Used: CLAUDE.md (source)

    Artifacts

    Evidence from the check

    • Captured the exact Go test source used to feed the converter the claimed event order, providing the reproducible input.

    Command output from the check

    • Executed the focused converter test; it records reasoning at index 0, function call at index 1, and assistant message at index 1 before failing, confirming the collision.

    View artifacts

    T-Rex Ran code and verified through T-Rex

  2. General comment

    P1 Started tool call and later assistant message share Responses output index

    • Bug
      • For an upstream stream that emits reasoning, starts tool call 0, then emits assistant content, the converter produces response.output_item.added for the function call and assistant message with output_index: 1. Responses output items therefore collide at the same output index.
    • Cause
      • reserveAssistantOutput at internal/providers/responses_converter.go:146-155 increments indexes only for tool-call states that have not started. Once handleToolCallDeltas starts the tool call at index 1, subsequent assistant text reserves its fixed post-reasoning index 1 without moving the started tool call or allocating a distinct assistant index.
    • Fix
      • Allocate the assistant output index according to preceding/started tool-call output items, or reject/otherwise serialize assistant text after a started tool call. Add the executed reasoning → tool-call → text regression case to the permanent converter suite.

    T-Rex Ran code and verified through T-Rex

Reviews (1): Last reviewed commit: "fix(responses): preserve reasoning acros..." | Re-trigger Greptile

Copilot AI review requested due to automatic review settings July 31, 2026 14:07

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SantiagoDePolonia

Copy link
Copy Markdown
Contributor Author

Addressed the review findings in 7abcc27:

  • reasoning → started function call → assistant text now assigns output indexes 0, 1, and 2 respectively; emitted items are never renumbered
  • usage-field presence is validated from the decoded top-level object without pointer allocations
  • added regression coverage for both cases
  • updated the Groq Responses-stream contract golden to expect Responses usage field names

Validation passed: go test ./..., contract replay tests, race tests, lint, and the hot-path performance guard (87 allocations/op, 10,296 bytes/op).

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/providers/responses_converter_test.go`:
- Around line 286-315: Convert the new assistant-after-started-tool case at
internal/providers/responses_converter_test.go:286-315 into an entry in the
relevant table-driven converter test, preserving its expected output indexes.
Also convert the nested required usage-field omission case at
internal/providers/responses_converter_test.go:597-620 into an entry in the
appropriate table-driven test, retaining its expected behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3fb82e59-1a06-4c00-891f-01e7d933cb0d

📥 Commits

Reviewing files that changed from the base of the PR and between 0ee3a91 and 7abcc27.

📒 Files selected for processing (3)
  • internal/providers/responses_converter.go
  • internal/providers/responses_converter_test.go
  • tests/contract/testdata/golden/groq/responses_stream.golden.json

Comment thread internal/providers/responses_converter_test.go Outdated
Copilot AI review requested due to automatic review settings July 31, 2026 14:16

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/providers/responses_converter_test.go`:
- Around line 269-285: Update the test’s event tracking around the
response.output_item.added handling to preserve insertion order in an addedTypes
slice instead of relying only on addedIndexes. Add the expected added-item
sequence to each table-driven case, then assert the observed sequence; for
“assistant after started tool call,” require reasoning, function_call, then
message while retaining the existing index and reasoning assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 0fcf1267-4ceb-488e-9cd1-7c4207de3dc2

📥 Commits

Reviewing files that changed from the base of the PR and between 7abcc27 and ac8ccf5.

📒 Files selected for processing (1)
  • internal/providers/responses_converter_test.go

Comment thread internal/providers/responses_converter_test.go
Copilot AI review requested due to automatic review settings July 31, 2026 14:21

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SantiagoDePolonia SantiagoDePolonia changed the title fix(responses): preserve reasoning across Codex tool turns fix(responses): preserve DeepSeek reasoning across tool turns Jul 31, 2026
@SantiagoDePolonia
SantiagoDePolonia merged commit c0b5d40 into main Jul 31, 2026
19 checks passed
@SantiagoDePolonia
SantiagoDePolonia deleted the fix/codex2 branch July 31, 2026 15:13
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.

3 participants