fix: ensure the BuiltInTools entry exists before incrementing CallCount - #1754
Conversation
…nse in stream handler
WalkthroughAdds 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
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
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Assessment against linked issues
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate 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. Comment |
There was a problem hiding this comment.
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 lookupThis 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"]))andsafeIncBuiltInTool(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.
📒 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.completedThe added guard on streamResponse.Response and .Usage addresses the reported panic path. LGTM.
fix: ensure the BuiltInTools entry exists before incrementing CallCount
Fix: #1748
在操作前进行检查,解决因为工具值无法转换,或者异常结构导致返回空值,在空值上操作导致程序panic
Summary by CodeRabbit