Skip to content

fix(openai): reject malformed Responses reasoning item IDs - #6678

Open
ZHOUKAILIAN wants to merge 2 commits into
QuantumNous:mainfrom
ZHOUKAILIAN:fix/responses-reasoning-item-id
Open

fix(openai): reject malformed Responses reasoning item IDs#6678
ZHOUKAILIAN wants to merge 2 commits into
QuantumNous:mainfrom
ZHOUKAILIAN:fix/responses-reasoning-item-id

Conversation

@ZHOUKAILIAN

@ZHOUKAILIAN ZHOUKAILIAN commented Aug 6, 2026

Copy link
Copy Markdown

📝 变更描述 / Description

The direct OpenAI Responses relay currently forwards successful upstream reasoning items without validating their IDs. If an upstream emits {"type":"reasoning","id":"item_..."}, clients can persist that output and replay it later, where a conforming endpoint rejects it because reasoning item IDs must use the rs_ prefix.

This change validates reasoning output items before forwarding them:

  • Non-streaming responses return bad_response_body with HTTP 502 before writing the malformed body.
  • Streaming responses stop before writing a malformed response.output_item.* event, and also validate reasoning items in terminal response.output arrays.
  • Valid rs_ IDs and non-reasoning item IDs are unchanged.
  • Opaque upstream IDs are rejected rather than rewritten.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 已关联对应 Issue。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行相关测试。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

Passed:

go test ./relay/channel/openai
go test -race ./relay/channel/openai
go test ./relay/...

go test ./... was also run. All discovered packages passed except the root package setup, which requires the generated web/dist directory that is absent in a clean backend worktree:

main.go:42:12: pattern web/dist: no matching files found

Summary by CodeRabbit

  • Bug Fixes

    • Responses containing malformed reasoning item IDs are now rejected instead of being forwarded.
    • Streaming responses stop processing when an invalid reasoning item is detected and return an appropriate error.
    • Valid reasoning items and other response types continue to be handled normally.
    • Missing or incorrectly formatted reasoning IDs now produce clear API errors.
  • Tests

    • Added coverage for valid and invalid reasoning IDs in both streaming and non-streaming responses.
    • Added checks confirming terminal and non-terminal invalid items are handled safely.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a1308f41-d2a9-4094-954c-c7d488d58d5f

📥 Commits

Reviewing files that changed from the base of the PR and between 1f2dcbb and 3d59ccb.

📒 Files selected for processing (1)
  • relay/channel/openai/relay_responses_validation_test.go

Walkthrough

The OpenAI Responses relay now validates reasoning output IDs in batch and streamed responses. IDs must use the rs_ prefix. Invalid items produce bad-response errors and are not forwarded. Tests cover valid, invalid, streaming, and non-streaming cases.

Changes

Responses validation

Layer / File(s) Summary
Reasoning ID validation contract
relay/channel/openai/relay_responses.go, relay/channel/openai/relay_responses_validation_test.go
Reasoning outputs must use rs_-prefixed IDs. Non-reasoning and nil outputs remain valid.
Batch and stream handler enforcement
relay/channel/openai/relay_responses.go, relay/channel/openai/relay_responses_validation_test.go
Batch responses reject invalid outputs before writing the body. Streaming responses stop before forwarding invalid items and return the stored error. Tests verify rejection and valid forwarding.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Upstream
  participant ResponsesRelay
  participant Client
  Upstream->>ResponsesRelay: Send Responses payload or stream event
  ResponsesRelay->>ResponsesRelay: Validate reasoning item ID
  ResponsesRelay-->>Client: Forward valid data
  ResponsesRelay-->>Client: Return bad-response error for invalid data
Loading

Possibly related PRs

Poem

A rabbit checks each reasoning ID,
“rs_” must hop along inside.
Bad ones stop before they run,
Valid streams reach everyone.
Tests thump paws: the guard is done.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: rejecting malformed OpenAI Responses reasoning item IDs.
Linked Issues check ✅ Passed The changes implement reasoning ID validation for JSON and SSE responses, reject invalid IDs, and preserve valid and non-reasoning items as required by issue [#6677].
Out of Scope Changes check ✅ Passed The implementation and tests are limited to reasoning ID validation in the OpenAI Responses relay and directly support issue [#6677].
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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 `@relay/channel/openai/relay_responses_validation_test.go`:
- Around line 93-138: Extend
TestOaiResponsesStreamHandlerStopsBeforeInvalidReasoningItem with a terminal
response.completed or response.done SSE event whose response.output contains the
invalid reasoning item item_bad. Assert OaiResponsesStreamHandler returns the
existing HTTP 502 and that the terminal event, including item_bad, is not
written to w.Body.
🪄 Autofix

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: CHILL

Plan: Pro Plus

Run ID: f719f8c5-3704-4f6e-bd30-eaa58bc67cfd

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab0202 and 1f2dcbb.

📒 Files selected for processing (2)
  • relay/channel/openai/relay_responses.go
  • relay/channel/openai/relay_responses_validation_test.go

Comment thread relay/channel/openai/relay_responses_validation_test.go
@coderabbitai coderabbitai Bot mentioned this pull request Aug 15, 2026
5 tasks
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.

Validate reasoning item IDs in OpenAI Responses relay output

1 participant