Skip to content

ollama: add native tool calling support#1491

Open
majiayu000 wants to merge 1 commit intotmc:mainfrom
majiayu000:feat/issue-1490-ollama-tool-calling
Open

ollama: add native tool calling support#1491
majiayu000 wants to merge 1 commit intotmc:mainfrom
majiayu000:feat/issue-1490-ollama-tool-calling

Conversation

@majiayu000
Copy link
Copy Markdown
Contributor

PR Checklist

  • Read the Contributing documentation.
  • Read the Code of conduct documentation.
  • Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as memory: add interfaces for X, Y or util: add whizzbang helpers).
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. Fixes #123).
  • Describes the source of new concepts.
  • References existing implementations as appropriate.
  • Contains test coverage for new functions.
  • Passes all golangci-lint checks.

Fixes #1490

Adds native tool calling to the Ollama provider so models like Gemma 4 can use structured tool calls through the Ollama chat API.

What changed:

types.go - Added Tool, ToolFunction, ToolCall, and ToolCallFunction structs matching Ollama's API format. ToolCallFunction has custom JSON unmarshaling since Ollama returns arguments as an object but llms.FunctionCall expects a string. Added Tools field to ChatRequest and ToolCalls field to Message.

ollamallm.go - Extracted message construction into makeOllamaMessages to handle ToolCall and ToolCallResponse content parts (tool results get role=tool). Added makeOllamaTools to convert llms.Tool to ollamaclient.Tool. Response handling now converts ToolCall responses back to llms.ToolCall. Streaming is disabled when tools are present since Ollama doesn't support it.

ollama_test.go - Added TestToolCall with httprr recording covering the full flow: send message with tool defs, get tool call back, send tool result, get final text response.

Differs from the earlier #1022 attempt in a few ways: tool responses use role=tool correctly (users reported the model wasn't seeing responses in #1022), message construction is split out to keep complexity down, and tools are passed via llms.WithTools() instead of a provider-specific option.

Add tool calling support to the Ollama provider, enabling models like
Gemma 4 to use structured tool calls through the Ollama chat API.

Changes:
- Add Tool, ToolCall, ToolCallFunction types to ollamaclient with
  custom JSON marshaling (Ollama sends arguments as object, not string)
- Add Tools field to ChatRequest and ToolCalls to Message
- Handle ToolCall and ToolCallResponse content parts in message
  conversion
- Convert response tool_calls back to llms.ToolCall
- Disable streaming when tools are present (Ollama limitation)
- Preserve tool_calls through the response callback

Signed-off-by: majiayu000 <1835304752@qq.com>
@majiayu000 majiayu000 marked this pull request as ready for review April 8, 2026 09:02
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.

Support Gemma 4 tool calling token pairs

1 participant