Skip to content

fix: normalize null Responses input content#4626

Open
u-wlkjyy wants to merge 2 commits intoQuantumNous:mainfrom
u-wlkjyy:codex/upstream-normalize-null-responses-input
Open

fix: normalize null Responses input content#4626
u-wlkjyy wants to merge 2 commits intoQuantumNous:mainfrom
u-wlkjyy:codex/upstream-normalize-null-responses-input

Conversation

@u-wlkjyy
Copy link
Copy Markdown

@u-wlkjyy u-wlkjyy commented May 5, 2026

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

Some OpenAI-compatible clients can send empty assistant messages as content: null. When these requests are converted to, or directly sent through, the Responses API path, Responses-compatible upstreams reject them because each input item's content must be a string or an array.

This change normalizes explicit null content on Responses input items to an empty string before the request is sent upstream. It is applied both to native /v1/responses requests and to the /v1/chat/completions to /v1/responses compatibility conversion path.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (N/A)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

Relevant test passed:

go test ./dto
ok  	github.com/QuantumNous/new-api/dto	0.025s

I also attempted broader local test commands. They are currently blocked in this workspace by unrelated existing/local issues:

go test ./...
FAIL github.com/QuantumNous/new-api [setup failed]
main.go:43:12: pattern web/classic/dist: no matching files found

FAIL github.com/QuantumNous/new-api/output [build failed]
output/analytics_paper_bench.go: undefined symbols from local untracked output files

A targeted relay command also reaches unrelated existing failures in Claude and stream scanner tests, outside the files changed by this PR.

Summary by CodeRabbit

  • Bug Fixes

    • Normalize null message content in incoming requests to empty strings, improving robustness and compatibility of API input handling.
  • Tests

    • Added tests verifying null content is correctly preserved or converted where appropriate, reducing regressions.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 05ac9299-ed5d-43f2-87fd-90c3d3e60e89

📥 Commits

Reviewing files that changed from the base of the PR and between e15435a and 9880498.

📒 Files selected for processing (2)
  • dto/openai_request.go
  • dto/openai_request_zero_value_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • dto/openai_request.go

Walkthrough

Adds OpenAIResponsesRequest.NormalizeInputNullContent() to rewrite message-like input entries with content: null to "", integrates that normalization into request handling and conversion paths, and adds a unit test validating the behavior.

Changes

Responses API Null Content Normalization

Layer / File(s) Summary
Data Shape / Import
dto/openai_request.go
Adds bytes import to support trimming when reserializing normalized JSON.
Core Normalization Logic
dto/openai_request.go
Implements OpenAIResponsesRequest.NormalizeInputNullContent() which unmarshals Input (JSON array), replaces message-like items with content: null to "", and re-marshals/stores trimmed JSON back to Input.
Handler Integration
relay/responses_handler.go
After deep-copying the request, ResponsesHelper calls request.NormalizeInputNullContent() and returns an invalid_request error (with skip-retry) if normalization fails.
Conversion Integration
service/openaicompat/chat_to_responses.go
ChatCompletionsRequestToResponsesRequest invokes out.NormalizeInputNullContent() after building the responses request and returns any error.
Tests
dto/openai_request_zero_value_test.go
Adds TestOpenAIResponsesRequestNormalizeInputNullContent asserting that input[1].content converts from JSON null to "", while other entries remain unchanged as expected.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • seefs001
  • Calcium-Ion

Poem

🐰 I hopped through JSON, quiet and small,
Turned null to a whisper, no crash nor call,
Empty strings now softly sing,
Requests march on like spring—
A tidy fix, I nibbled and solved it all.

🚥 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 accurately describes the main change: normalizing null Responses input content to empty strings before sending upstream.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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 and usage tips.

@u-wlkjyy
Copy link
Copy Markdown
Author

u-wlkjyy commented May 6, 2026

trea cn调用会存在这个问题

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