Skip to content

fix(openai): add nil checks for web_search streaming to prevent panic - #1949

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
RedwindA:fix/oai-responses-webSearch-panic
Oct 1, 2025
Merged

fix(openai): add nil checks for web_search streaming to prevent panic#1949
seefs001 merged 1 commit into
QuantumNous:mainfrom
RedwindA:fix/oai-responses-webSearch-panic

Conversation

@RedwindA

@RedwindA RedwindA commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

PR 类型

  • Bug 修复
  • 新功能
  • 文档更新
  • 其他

PR 是否包含破坏性更新?

PR 描述

close #1933

Summary by CodeRabbit

  • Bug Fixes
    • Resolved a rare crash that could occur when tracking usage for the web search preview tool, improving overall stability.
    • Added safeguards to handle missing usage data gracefully, preventing unexpected errors during response processing.
    • Ensures consistent behavior for existing workflows; no changes to normal functionality are expected.

@coderabbitai

coderabbitai Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds defensive nil checks before incrementing a built-in tool’s CallCount in OpenAI relay responses to prevent nil dereference when usage info or map entries are absent.

Changes

Cohort / File(s) Summary of Changes
Defensive nil checks for built-in tool usage
relay/channel/openai/relay_responses.go
Wrapped CallCount increment for dto.BuildInToolWebSearchPreview with checks for info, info.ResponsesUsageInfo, BuiltInTools map presence, key existence, and non-nil tool entry before incrementing. No other logic altered.

Sequence Diagram(s)

sequenceDiagram
    participant Handler as OpenAI Relay Handler
    participant Usage as ResponsesUsageInfo
    participant Tools as BuiltInTools Map
    participant Tool as WebSearchPreview Tool

    Handler->>Usage: Access usage info (may be nil)
    alt Usage or map nil
        Handler-->>Handler: Skip increment
    else Usage and map exist
        Handler->>Tools: Lookup WebSearchPreview key
        alt Key missing or Tool nil
            Handler-->>Handler: Skip increment
        else Key present and Tool non-nil
            Handler->>Tool: Increment CallCount
        end
    end
    note over Handler: Prevents nil dereference panics
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

A whisker twitch, a careful hop,
I guard the map—no panic pop!
If tools aren’t there, I softly pass,
When they are, I count—so fast!
Burrow stable, logs serene,
Bugs be gone; my code stays clean. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly states that this PR is a fix for the OpenAI integration by adding nil checks in the web_search streaming code to prevent a panic, which aligns precisely with the primary change implemented in the diff. It concisely conveys the nature of the update without extraneous detail and specifies the component affected. As a result, it allows readers to immediately understand the purpose and impact of the change.
Linked Issues Check ✅ Passed The PR adds nil checks around info.ResponsesUsageInfo and its BuiltInTools map before incrementing the CallCount for the web_search preview tool, which directly addresses the panic scenario reported in issue #1933 by guarding against nil dereferences. The change precisely targets the streaming logic where the crash occurred and restores normal return behavior when structures are missing. This implementation fulfills the linked issue’s requirement to prevent service crashes and return results normally.
Out of Scope Changes Check ✅ Passed All modifications are confined to relay/channel/openai/relay_responses.go to implement the nil guards for web_search streaming and no other files or unrelated logic have been altered. The PR contains only the intended bug fix without additional refactoring or feature additions. Consequently, there are no out-of-scope changes present.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 96b172e and 2200bb9.

📒 Files selected for processing (1)
  • relay/channel/openai/relay_responses.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/openai/relay_responses.go (2)
relay/common/relay_info.go (1)
  • ResponsesUsageInfo (50-52)
dto/openai_response.go (1)
  • BuildInToolWebSearchPreview (337-337)
🔇 Additional comments (1)
relay/channel/openai/relay_responses.go (1)

118-122: Defensive nil checks correctly prevent panic.

The nested nil checks follow the same defensive pattern as the non-streaming handler (lines 55-66) and will prevent the panic described in issue #1933. The logic correctly verifies each level of the structure before accessing the CallCount field.

One minor observation: unlike the non-streaming handler (line 62), there's no error logging when the tool is not found. This appears intentional since the tool may legitimately be absent in some streaming scenarios, but consider adding debug-level logging if troubleshooting tool usage becomes necessary in the future.


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.

@seefs001
seefs001 merged commit 2938246 into QuantumNous:main Oct 1, 2025
1 check passed
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
…ebSearch-panic

fix(openai): add nil checks for web_search streaming to prevent panic
@RedwindA
RedwindA deleted the fix/oai-responses-webSearch-panic branch July 23, 2026 07:04
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.

OPENAI 在调用模型内置搜索引擎时 newapi 崩溃重启

2 participants