Skip to content

[integration][java] Forward assistant tool calls in the Ollama chat history - #1166

Merged
wenjin272 merged 1 commit into
apache:mainfrom
Zhuoxi2000:fix-ollama-java-tool-call-history
Sep 29, 2026
Merged

wenjin272 merged 1 commit into
apache:mainfrom
Zhuoxi2000:fix-ollama-java-tool-call-history

Conversation

@Zhuoxi2000

Copy link
Copy Markdown
Contributor

Linked issue: #1165

Purpose of change

A Java agent on Ollama now sends the assistant's earlier tool calls back with the chat history, so the model sees the calls that produced the tool results, as a Python agent already does. Before, convertToOllamaChatMessages kept only each message's role and text.

Runtime flow: buildRequest converts each message; an assistant message with tool calls now gets tool_calls built from each call's function name and arguments. Key decisions: match the Python connection, which sends the name and the arguments object and no id; the framework-assigned id is unknown to Ollama. Arguments that arrive as a JSON string are parsed into an object, since Ollama expects one.

Behavioral Semantics

Interaction decisions: not applicable; one condition (a message has tool calls or not).

Behavioral contracts:

  1. A message with tool calls is sent with tool_calls, one per call, in order, each with function.name and function.arguments as an object.
  2. Arguments given as a JSON string are sent as the parsed object.
  3. A message without tool calls is sent as before.

Failure behavior: a tool call without a function name, or with arguments that are neither an object nor a JSON object string, throws IllegalArgumentException while the request is built; nothing is sent.

Tests

Contract Test
1, 2, 3 OllamaChatModelConnectionTest#buildRequestForwardsAssistantToolCalls, asserting on the request as serialized by ollama4j

Not verified: a live multi-turn tool call against an Ollama server; the failure paths above have no dedicated test. Module tests pass locally (16/16), with spotless.

API

No API change. Requests for histories with tool calls now include them; other requests are unchanged.

Documentation

  • doc-needed
  • doc-not-needed
  • doc-included

Was this patch authored or co-authored using generative AI tooling?

  • Yes
  • No

If yes, include a Generated-by: <tool name and version> (<model name and version>) line, for example Generated-by: Claude Code 2.1.226 (Claude Opus 4.6), in the commit message so it reaches Git history. Repeat the same line here for reviewer visibility. See the ASF generative tooling guidance.

Generated-by: Claude Code 2.1.259 (Claude Opus 5.5)

@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs fixVersion/0.4.0 priority/major Default priority of the PR or issue. labels Sep 28, 2026

@wenjin272 wenjin272 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.

Thanks for taking this on. LGTM

@wenjin272
wenjin272 merged commit 97718d7 into apache:main Sep 29, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs fixVersion/0.4.0 priority/major Default priority of the PR or issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants