Skip to content

Fix Ollama tool call round trips - #21905

Open
wykthreed wants to merge 1 commit into
infiniflow:mainfrom
wykthreed:fix/ollama-tool-calls
Open

wykthreed wants to merge 1 commit into
infiniflow:mainfrom
wykthreed:fix/ollama-tool-calls

Conversation

@wykthreed

Copy link
Copy Markdown

Summary

Ollama tool calls are currently dropped from both streaming and non-streaming chat responses. Tool-only responses can also fail with "no message in response", preventing the agent from executing retrieval and other tools.

This change preserves returned tool calls, supplies IDs when Ollama omits them, and converts arguments between Ollama's JSON objects and the runtime's JSON strings. Follow-up requests send tool results using Ollama's tool_name field. Incomplete streams do not publish tool calls.

Validation

  • Ran bash build.sh --cpp and bash build.sh --test ./internal/entity/models/... in the ARM64 Docker build environment; model package and native unit tests passed.
  • Added HTTP-stub round-trip tests through the Eino adapter for streaming and non-streaming calls, including parallel calls, tool results and a final answer.
  • Added coverage for malformed arguments, incomplete streams, call IDs and usage reporting.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

Ollama chat requests and responses now support tool calls. Request construction validates tool-call arguments and maps tool replies. Response handling converts tool calls for streaming and non-streaming chats.

Changes: Ollama tool-call handling

Layer / File(s) Summary
Request conversion and error propagation
internal/entity/models/ollama.go, internal/entity/models/ollama_tool_test.go
Request construction converts tool calls and tool replies. String-encoded arguments must decode to a non-nil JSON object. Chat methods propagate request-construction errors. Tests cover malformed arguments.
Response conversion and stream results
internal/entity/models/ollama.go, internal/entity/models/ollama_tool_test.go
Response handling converts tool calls, accepts tool-call-only non-streaming responses, and stores calls after a completed stream. Tests cover streaming and non-streaming round trips, incomplete streams, parallel calls, and token usage.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant OllamaChat as Ollama chat method
  participant OllamaAPI as Ollama API
  participant ChatConfig as chatConfig
  Caller->>OllamaChat: Submit chat messages and tools
  OllamaChat->>OllamaAPI: Send converted request
  OllamaAPI-->>OllamaChat: Return response tool calls
  OllamaChat->>ChatConfig: Set ToolCallsResult after stream completes
Loading

Possibly related PRs

  • infiniflow/ragflow#21444: Preserves ReAct tool calls in agent output and memory, which can consume tool calls returned by this change.

Suggested labels: 🐞 bug, go

Suggested reviewers: jinhai-cn, lynn-inf

Merge Risk: Minimal · up to b1a00

The tool-call round-trip change is mergeable after normal checks; no concrete blocking issue remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title "Fix Ollama tool call round trips" matches the main change in the changeset. The raw summary and PR objectives show the change restores tool calls in streaming and non-streaming Ollama respo…
Description check Passed The description follows the template's Summary section. It states the problem: Ollama tool calls are dropped from streaming and non-streaming responses, and tool-only responses fail with "no message i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Fix Ollama tool call round trips" matches the main change in the changeset. The raw summary and PR objectives show the change restores tool calls in streaming and non-streaming Ollama responses, converts arguments, and sends tool results in follow-up requests. The title is short, specific, and free of noise.

Full details: Description check

Explanation

The description follows the template's Summary section. It states the problem: Ollama tool calls are dropped from streaming and non-streaming responses, and tool-only responses fail with "no message in response". It also explains the fix: preserving tool calls, supplying missing IDs, converting arguments between JSON objects and strings, sending results with tool_name, and not publishing tool calls from incomplete streams. It adds a Validation section with build commands and test coverage. The template only requires a Summary, so the description is complete.

Full details: Docstring Coverage

Explanation

Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 9 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

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

A rabbit checks the calls in flight,
Then gathers tools in JSON right.
Two paths return what Ollama sends,
A stream completes; the call list blends.
The rabbit hops, its tests all green.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fd435b3a-4532-4527-99a1-4b19111cd1fc
Commits

Reviewing files that changed from the base of the PR and between 62a4eae and b1a0097.

Files selected for processing: 2
  • internal/entity/models/ollama.go
  • internal/entity/models/ollama_tool_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Harsh23Kashyap Harsh23Kashyap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified at b1a0097. The round trip works in both directions against a stub server, streaming and non-streaming: outbound history rewrites tool messages to the native shape (tool_name set, tool_call_id dropped, arguments sent as objects), inbound calls get runtime IDs with JSON-encoded arguments, malformed argument strings fail closed, and an incomplete stream publishes nothing.

Proof: all 12 Ollama tests pass in-tree at this head. Against current main the round trip does not exist: a response whose message carries only tool_calls errors with "no message in response", and tool-role history goes out with tool_call_id instead of tool_name. Both confirmed with direct probes on main.

LGTM.

@Lynn-Inf Lynn-Inf 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.

The fix is right: tool-only responses no longer trip the "no message in response" check, streaming publishes calls only after done, and the request side converts arguments to Ollama's object shape and uses tool_name for results.

One gap on the response side: ollamaToolCall.Function.Arguments decodes as map[string]any, so a server that returns OpenAI-style string arguments ("arguments": "{\"query\":\"x\"}") fails json.Unmarshal for the entire response. Before this change that field was not parsed at all, so the same request succeeded. The request path already accepts both shapes, so please mirror it here — decode Arguments as json.RawMessage and accept either an object or a JSON string (normalising an empty string to {}), then marshal to the internal form as you do today.

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

Verified this at b1a0097. On the response side ollamaToolCall.Function.Arguments is typed map[string]any, so a server that returns OpenAI-style string arguments fails the json.Unmarshal for the whole response, and before this change that field was ignored entirely so the same response went through. Real strictness regression for non-conforming servers. The request path (ollama.go:78-86) already accepts both shapes, so mirroring it with json.RawMessage and normalizing an empty string to {} is a small, symmetric fix. Worth taking before merge.

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.

4 participants