fix: handle ollama non-stream tool calls - #5865
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughOllama chat handling now uses shared JSON helpers, a typed tool-call shape, and a common conversion path for stream and non-stream responses. Non-stream output now preserves tool calls in the final message and sets ChangesOllama tool call handling
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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
🧹 Nitpick comments (1)
relay/channel/ollama/stream_test.go (1)
18-72: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd coverage for the single-chunk fallback path.
The
rawbody is a single-line JSON, so it's consumed by the multi-line loop's success path (parsedAny=true); the!parsedAnyfallback branch inollamaChatHandler(stream.go lines 265-294), which has its own independentollamaToolCallsToOpenAIcall forsingle.Message.ToolCalls, remains untested.Consider adding a second table case with a body that fails per-line JSON parsing (e.g. pretty-printed/multi-line JSON) to exercise the fallback branch's tool-call aggregation.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@relay/channel/ollama/stream_test.go` around lines 18 - 72, The current test only covers the multi-line parsing success path in ollamaChatHandler, leaving the single-chunk fallback branch untested. Add a second test case (or table entry) in TestOllamaChatHandlerNonStreamToolCalls that uses a body format causing per-line JSON parsing to fail, so the !parsedAny fallback path is exercised. Verify that the fallback still aggregates tool calls correctly via ollamaToolCallsToOpenAI on single.Message.ToolCalls, including finish reason, tool-call contents, and the absence of Index for non-stream tool calls.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@relay/channel/ollama/stream_test.go`:
- Around line 1-72: The new test in ollamaChatHandlerNonStreamToolCalls uses
manual t.Fatalf checks instead of the required testify helpers. Update this test
to import github.com/stretchr/testify/require and
github.com/stretchr/testify/assert, use require for setup and error/fatal checks
around ollamaChatHandler, common.Unmarshal, and response decoding, and use
assert for value comparisons on usage, FinishReason, tool call fields, and
arguments. Keep the test logic the same but replace the hand-rolled if blocks
with the appropriate require/assert calls throughout the function.
---
Nitpick comments:
In `@relay/channel/ollama/stream_test.go`:
- Around line 18-72: The current test only covers the multi-line parsing success
path in ollamaChatHandler, leaving the single-chunk fallback branch untested.
Add a second test case (or table entry) in
TestOllamaChatHandlerNonStreamToolCalls that uses a body format causing per-line
JSON parsing to fail, so the !parsedAny fallback path is exercised. Verify that
the fallback still aggregates tool calls correctly via ollamaToolCallsToOpenAI
on single.Message.ToolCalls, including finish reason, tool-call contents, and
the absence of Index for non-stream tool calls.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2443349d-296a-4833-a11b-12da748aae4f
📒 Files selected for processing (2)
relay/channel/ollama/stream.gorelay/channel/ollama/stream_test.go
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me> (cherry picked from commit 0977965)
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me> (cherry picked from commit 0977965)
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me> (cherry picked from commit 0977965)
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
* fix: handle ollama non-stream tool calls * test: cover ollama non-stream tool call paths --------- Co-authored-by: CaIon <i@caion.me>
Important
📝 变更描述 / Description
(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)
本次变更增加了新功能,支持了 Ollama 非流式响应中工具调用的功能。
Ollama 在
/api/chat非流式模式下返回工具调用时,会把结果放在message.tool_calls中,但原有的ollamaChatHandler只聚合了message.content、response和 reasoning 内容,没有读取并转换message.tool_calls,因此最终返回给 OpenAI 兼容客户端的响应中会丢失工具调用信息,finish_reason也仍然是上游的stop。本次修改将 Ollama 的
tool_calls统一转换为 OpenAI 兼容的ToolCallResponse结构,这样客户端在收到非流式响应时,可以正确识别模型触发了工具调用,并继续执行后续 tool call 流程。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
(请在此粘贴截图、关键日志或测试报告,以证明变更生效)
使用真实 Ollama 上游进行了验证:
http://127.0.0.1:11434qwen3:8b/api/chat"stream": falsemessage.tool_calls,new-api 应正确转换为 OpenAI 兼容的message.tool_calls,并将finish_reason设置为"tool_calls"。执行命令:
OLLAMA_BASE_URL=http://127.0.0.1:11434 OLLAMA_MODEL=qwen3:8b \ go test ./relay/channel/ollama -run TestLiveOllamaNonStreamToolCallsThroughHandler -count=1 -v补丁前,在
upstream/main(8874d192)上,同一个 live regression test 会失败:补丁后,在当前提交
5f183a48上,同一个 live regression test 通过:Summary by CodeRabbit