Skip to content

fix(ollama): persist assistant response in message_history to fix multi-turn tool calling - #427

Open
nankingjing wants to merge 3 commits into
bytedance:mainfrom
nankingjing:fix/ollama-chat-history
Open

fix(ollama): persist assistant response in message_history to fix multi-turn tool calling#427
nankingjing wants to merge 3 commits into
bytedance:mainfrom
nankingjing:fix/ollama-chat-history

Conversation

@nankingjing

Copy link
Copy Markdown

Problem

The OllamaClient.chat() method never persists the assistant's response (text or tool calls) in self.message_history. All other LLM clients (AnthropicClient, OpenAIClient, OpenAICompatibleClient, GoogleClient) correctly append the assistant response to their message history after each chat call, but the Ollama client was missing this step.

This breaks multi-turn conversations with tool calling because:

  1. The agent loop in BaseAgent._tool_call_handler only passes new tool result messages to subsequent chat() calls
  2. The LLM client is expected to maintain the full conversation history via self.message_history (with reuse_history=True as default)
  3. Without the assistant's tool-call response in history, the LLM sees an incomplete conversation: user asks task -> user provides tool results (missing the assistant's decision to call tools)

Fix

Added persistence of the assistant response to self.message_history after receiving the LLM response:

  • Text responses: appended as EasyInputMessageParam(role="assistant", content=..., type="message")
  • Tool call responses: appended as ResponseFunctionToolCallParam(call_id=..., name=..., arguments=..., type="function_call") for each tool call

Also added EasyInputMessageParam to the imports from openai.types.responses.

This follows the same pattern used by all other LLM client implementations.

Verification

  • Verified by reading all 5 LLM client implementations -- only OllamaClient was missing history persistence
  • Identified via grep confirming zero self.message_history.append() calls in OllamaClient vs multiple in every other client
  • Not executed (tests require a running Ollama server with model)

@nankingjing

Copy link
Copy Markdown
Author

Reviewed — persisting assistant responses in message_history is the correct fix for multi-turn Ollama conversations. Ready for review.

The previous fix used OpenAI  items for tool-call history,
but  expects  for assistant tool
turns and  for text turns. The mismatch caused subsequent
ollama.chat calls to reject the conversation history and broke multi-turn
tool calling.

- Record assistant tool-call turns as Ollama-native .
- Record assistant text turns as .
- Remove trailing blank line flagged by .
- Add regression tests covering both persistence shapes.
@nankingjing

Copy link
Copy Markdown
Author

Updated the fix to use the Ollama-native message shape for assistant turns instead of OpenAI records, which rejects during multi-turn tool calling. Also added regression tests covering both text-only and tool-call persistence. Tests and lint pass locally.

@nankingjing

Copy link
Copy Markdown
Author

Updated: the fix now persists assistant responses in Ollama-native message shape (role/tool_calls) instead of OpenAI function_call items, which ollama.chat rejects during multi-turn tool calling. Regression tests added for both text-only and tool-call persistence. @chao-peng @bytedance PTAL.

@nankingjing
nankingjing force-pushed the fix/ollama-chat-history branch from f14fd02 to cd68621 Compare July 16, 2026 14:34
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.

1 participant