Skip to content

fix: ensure the BuiltInTools entry exists before incrementing CallCount - #1754

Merged
seefs001 merged 2 commits into
QuantumNous:alphafrom
HynoR:fix/dtresp
Sep 7, 2025
Merged

fix: ensure the BuiltInTools entry exists before incrementing CallCount#1754
seefs001 merged 2 commits into
QuantumNous:alphafrom
HynoR:fix/dtresp

Conversation

@HynoR

@HynoR HynoR commented Sep 5, 2025

Copy link
Copy Markdown
Contributor

Fix: #1748
在操作前进行检查,解决因为工具值无法转换,或者异常结构导致返回空值,在空值上操作导致程序panic

Summary by CodeRabbit

  • Bug Fixes
    • Prevented rare crashes during streaming by guarding against missing response usage data.
    • Improved stability when calling built-in tools by safely handling unknown or absent tool entries.
    • Enhanced error logging for unsupported tool scenarios without interrupting requests.
    • Reduced nil-pointer risks in completion events for more reliable response handling.

@coderabbitai

coderabbitai Bot commented Sep 5, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds nil and existence guards in OpenAI response handlers: safely lookup BuiltInTools before incrementing CallCount in OaiResponsesHandler, and ensure streamResponse.Response is non-nil before accessing Usage in OaiResponsesStreamHandler to prevent nil pointer dereferences.

Changes

Cohort / File(s) Summary
OpenAI response handling guards
relay/channel/openai/relay_responses.go
- Early nil checks for info / info.ResponsesUsageInfo / BuiltInTools before returning current usage.
- Safe map lookup for tool types; log and continue if missing or nil; increment CallCount only when valid.
- In streaming handler, check streamResponse.Response is non-nil before reading .Usage on "response.completed".

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant OaiResponsesStreamHandler
  participant StreamResponse

  Client->>OaiResponsesStreamHandler: Stream event "response.completed"
  OaiResponsesStreamHandler->>StreamResponse: Check if Response exists
  alt Response is non-nil
    OaiResponsesStreamHandler->>StreamResponse: Read Response.Usage
    OaiResponsesStreamHandler-->>Client: Continue/finish
  else Response is nil
    note right of OaiResponsesStreamHandler #EFEFEF: Skip usage access to avoid nil deref
    OaiResponsesStreamHandler-->>Client: Continue/finish
  end
Loading
sequenceDiagram
  participant Request
  participant OaiResponsesHandler
  participant BuiltInTools

  Request->>OaiResponsesHandler: Handle tool usage data
  OaiResponsesHandler->>BuiltInTools: Safe lookup by tool type
  alt Tool exists and non-nil
    OaiResponsesHandler->>BuiltInTools: Increment CallCount
  else Tool missing or nil
    note right of OaiResponsesHandler #EFEFEF: Log error and continue without increment
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent panic in OpenAI streaming response handling by avoiding nil pointer dereference (#1748)

I twitch my whiskers at a nil that tried to bite,
I count the tools when they're present, not by fright.
Streams now check before they peek inside,
Logs keep calm, no panics to hide.
A rabbit hops on—safe code for the night. 🐰✨


📜 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 c0187d5 and a77a883.

📒 Files selected for processing (1)
  • relay/channel/openai/relay_responses.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/openai/relay_responses.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
relay/channel/openai/relay_responses.go (1)

101-103: Remaining panic risk: incrementing CallCount without safe lookup

This still dereferences a possibly nil map holder and/or nil entry. Mirror the guarded pattern used above.

-            info.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview].CallCount++
+            if info != nil && info.ResponsesUsageInfo != nil && info.ResponsesUsageInfo.BuiltInTools != nil {
+              if toolInfo, ok := info.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; ok && toolInfo != nil {
+                toolInfo.CallCount++
+              } else {
+                logger.LogError(c, "BuiltInTools not found or nil for web search tool")
+              }
+            } else {
+              logger.LogError(c, "ResponsesUsageInfo or BuiltInTools is nil; skip tool usage aggregation")
+            }
🧹 Nitpick comments (1)
relay/channel/openai/relay_responses.go (1)

49-57: Extract and use a safe increment helper for BuiltInTools.CallCount

  • Replace direct increments in both locations with a centralized helper:
    • Lines 49–57 (loop over responsesResponse.Tools)
    • Lines 99–104 (switch on streamResponse.Item.Type)
  • Add in this package:
func safeIncBuiltInTool(ctx context.Context, info *relaycommon.RelayInfo, key string) {
  if info == nil || info.ResponsesUsageInfo == nil || info.ResponsesUsageInfo.BuiltInTools == nil {
    logger.LogError(ctx, "ResponsesUsageInfo or BuiltInTools is nil; skip tool usage aggregation")
    return
  }
  if ti, ok := info.ResponsesUsageInfo.BuiltInTools[key]; ok && ti != nil {
    ti.CallCount++
    return
  }
  logger.LogError(ctx, fmt.Sprintf("BuiltInTools not found or nil for tool type: %s", key))
}
  • Update both sites to call safeIncBuiltInTool(c, info, common.Interface2String(tool["type"])) and safeIncBuiltInTool(c, info, dto.BuildInToolWebSearchPreview).
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3d0bf36 and c0187d5.

📒 Files selected for processing (1)
  • relay/channel/openai/relay_responses.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/openai/relay_responses.go (4)
relay/common/relay_info.go (1)
  • ResponsesUsageInfo (50-52)
common/utils.go (1)
  • Interface2String (118-136)
logger/logger.go (1)
  • LogError (63-65)
dto/openai_response.go (1)
  • Usage (217-230)
🔇 Additional comments (1)
relay/channel/openai/relay_responses.go (1)

80-93: Nice: nil-check prevents deref on response.completed

The added guard on streamResponse.Response and .Usage addresses the reported panic path. LGTM.

Comment thread relay/channel/openai/relay_responses.go
@seefs001
seefs001 merged commit d05974f into QuantumNous:alpha Sep 7, 2025
3 checks passed
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
fix: ensure the BuiltInTools entry exists before incrementing CallCount
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.

2 participants