Skip to content

Conversation

HynoR
Copy link
Contributor

@HynoR HynoR commented Sep 5, 2025

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

Summary by CodeRabbit

  • Bug Fixes
    • Prevented rare crashes when processing streaming responses by adding safeguards for missing usage data.
    • Improved stability when invoking built-in tools by safely handling unknown or missing tool types.
    • Enhanced error handling and logging for unsupported tool scenarios without interrupting requests.
    • Reduced risk of nil pointer errors in edge cases, ensuring smoother completion events and more reliable response handling.

Copy link
Contributor

coderabbitai bot commented Sep 5, 2025

Walkthrough

Adds guard checks in OpenAI response handlers: validates tool lookup before incrementing CallCount in OaiResponsesHandler, and adds a nil check for streamResponse.Response in OaiResponsesStreamHandler on "response.completed" to avoid nil pointer dereference.

Changes

Cohort / File(s) Summary
OpenAI response handling guards
relay/channel/openai/relay_responses.go
- Guarded lookup for BuiltInTools before incrementing CallCount, with error logging if tool type missing.
- In streaming handler, check Response is non-nil before accessing Usage on "response.completed".

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant OaiResponsesStreamHandler
  participant StreamResponse

  Client->>OaiResponsesStreamHandler: Stream event "response.completed"
  OaiResponsesStreamHandler->>StreamResponse: Access Response?
  alt Response is non-nil
    OaiResponsesStreamHandler->>StreamResponse: Read Response.Usage
    OaiResponsesStreamHandler-->>Client: Continue/finish
  else Response is nil
    note over OaiResponsesStreamHandler: 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
  OaiResponsesHandler->>BuiltInTools: Lookup tool by type
  alt Tool exists
    OaiResponsesHandler->>BuiltInTools: Increment CallCount
  else Tool missing
    note over OaiResponsesHandler: Log error and continue
  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)

Poem

I twitch my ears at streams that flow,
A sneaky nil? Not anymore—whoa!
Tools counted only when they’re found,
And usage checks stay safe and sound.
Hippity-hop, no crashes tonight,
The logs are calm, the code is right. 🐇✨

✨ 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 on lines +51 to +56
buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[common.Interface2String(tool["type"])]
if !ok {
logger.LogError(c, fmt.Sprintf("BuiltInTools not found for tool type: %v", tool["type"]))
continue
}
buildToolinfo.CallCount++
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Also guard nil map holder and nil entry before incrementing CallCount

Good start with the map lookup, but two panic paths remain: info.ResponsesUsageInfo (or its BuiltInTools) can be nil, and the map value itself can be a nil *BuildInToolInfo. Add both checks.

-    buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[common.Interface2String(tool["type"])]
-    if !ok {
-      logger.LogError(c, fmt.Sprintf("BuiltInTools not found for tool type: %v", tool["type"]))
-      continue
-    }
-    buildToolinfo.CallCount++
+    // Defensive: holder/map and entry must exist and be non-nil
+    if info == nil || info.ResponsesUsageInfo == nil || info.ResponsesUsageInfo.BuiltInTools == nil {
+      logger.LogError(c, "ResponsesUsageInfo or BuiltInTools is nil; skip tool usage aggregation")
+      continue
+    }
+    toolType := common.Interface2String(tool["type"])
+    buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[toolType]
+    if !ok || buildToolinfo == nil {
+      logger.LogError(c, fmt.Sprintf("BuiltInTools not found or nil for tool type: %v", tool["type"]))
+      continue
+    }
+    buildToolinfo.CallCount++
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[common.Interface2String(tool["type"])]
if !ok {
logger.LogError(c, fmt.Sprintf("BuiltInTools not found for tool type: %v", tool["type"]))
continue
}
buildToolinfo.CallCount++
// Defensive: holder/map and entry must exist and be non-nil
if info == nil || info.ResponsesUsageInfo == nil || info.ResponsesUsageInfo.BuiltInTools == nil {
logger.LogError(c, "ResponsesUsageInfo or BuiltInTools is nil; skip tool usage aggregation")
continue
}
toolType := common.Interface2String(tool["type"])
buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[toolType]
if !ok || buildToolinfo == nil {
logger.LogError(c, fmt.Sprintf("BuiltInTools not found or nil for tool type: %v", tool["type"]))
continue
}
buildToolinfo.CallCount++
🤖 Prompt for AI Agents
In relay/channel/openai/relay_responses.go around lines 51-56, the code assumes
info.ResponsesUsageInfo and its BuiltInTools map exist and that the looked-up
value is non-nil before incrementing CallCount; add guards: first check info !=
nil and info.ResponsesUsageInfo != nil and info.ResponsesUsageInfo.BuiltInTools
!= nil and handle the nil map (either initialize the map if mutation is allowed
or log/continue), then perform the map lookup, and if the retrieved
buildToolinfo is nil either allocate a new BuildInToolInfo struct and store it
back into the map before incrementing CallCount or log/continue to avoid a nil
dereference.

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