Skip to content

Python: Fix OllamaChatClient passing unsupported kwargs to ollama.AsyncClient… - #4403

Closed
Max Montes Soza (max-montes) wants to merge 6 commits into
microsoft:mainfrom
max-montes:fix/ollama-filter-unsupported-kwargs
Closed

Max Montes Soza (max-montes) wants to merge 6 commits into
microsoft:mainfrom
max-montes:fix/ollama-filter-unsupported-kwargs

Conversation

@max-montes

@max-montes Max Montes Soza (max-montes) commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Motivation and Context

Stop forwarding **kwargs from _inner_get_response() to ollama.AsyncClient.chat(). When the orchestration layer (e.g. HandoffBuilder) injects kwargs like allow_multiple_tool_calls=True, these were passed through unfiltered, causing a TypeError since ollama.AsyncClient.chat() does not accept them.

Description

This aligns with how other clients (e.g. OpenAIChatClient) handle kwargs — they only pass the prepared options_dict to the provider's API, not the raw **kwargs from the middleware chain.

Fixes #4402

Contribution Checklist

  • [ X] The code builds clean without any errors or warnings
  • [ X] The PR follows the Contribution Guidelines
  • [X ] All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings March 2, 2026 22:33
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Mar 2, 2026
@github-actions github-actions Bot changed the title Fix OllamaChatClient passing unsupported kwargs to ollama.AsyncClient… Python: Fix OllamaChatClient passing unsupported kwargs to ollama.AsyncClient… Mar 2, 2026

Copilot AI 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.

Pull request overview

Fixes the Ollama Python adapter to avoid forwarding orchestration/middleware-injected **kwargs into the Ollama SDK call (ollama.AsyncClient.chat()), preventing unexpected-keyword TypeErrors during workflows like handoffs.

Changes:

  • Stop passing _inner_get_response() **kwargs through to ollama.AsyncClient.chat() (streaming and non-streaming paths).
  • Add regression tests ensuring unsupported kwargs (e.g., allow_multiple_tool_calls) are not forwarded in either mode.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
python/packages/ollama/agent_framework_ollama/_chat_client.py Removes forwarding of arbitrary **kwargs into AsyncClient.chat(), aligning behavior with other provider adapters.
python/packages/ollama/tests/test_ollama_chat_client.py Adds regression coverage verifying unsupported kwargs are ignored for both streaming and non-streaming calls.

@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL22544279887% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python Unit Test Overview

Tests Skipped Failures Errors Time
4587 25 💤 0 ❌ 0 🔥 1m 20s ⏱️

@TaoChenOSU

Tao Chen (TaoChenOSU) commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

This looks like a duplicate of #4404. Please confirm and we can close this.

@max-montes
Max Montes Soza (max-montes) force-pushed the fix/ollama-filter-unsupported-kwargs branch from 7dad593 to 7984fbf Compare March 3, 2026 01:31
@max-montes

Max Montes Soza (max-montes) commented Mar 3, 2026 •

Copy link
Copy Markdown
Contributor Author

This looks like a duplicate of #4404. Please confirm and we can close this.

Hi Tao Chen (@TaoChenOSU) — this PR was actually submitted first (#4403 at 22:33 UTC vs
#4404 at 22:41 UTC).

Beyond timing, the approaches differ:

  • Python: fix(python): Filter unsupported kwargs in OllamaChatClient._inner_get_response #4404 uses a blocklist (_UNSUPPORTED_CHAT_KWARGS) to filter specific kwargs
    before chat() — but only covers the **kwargs path. New unsupported kwargs
    would need to be added to the set manually.
  • This PR removes **kwargs forwarding to chat() entirely (it was never
    needed) AND adds allow_multiple_tool_calls to exclude_keys in
    _prepare_options(), which covers the options dict path too (e.g., from
    Agent.default_options or workflow cloning). This is a more complete fix with
    less code.

Happy to make any adjustments you'd like, but if you've chose to go with the other fix, I'll go ahead and close.

@moonbox3 Evan Mattson (moonbox3) 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.

Automated Code Review

Reviewers: 3 | Confidence: 91%

✓ Correctness

The diff correctly fixes issue #4402 by stopping the forwarding of unsupported **kwargs to ollama.AsyncClient.chat() and adding allow_multiple_tool_calls to the set of keys excluded during option preparation. The three new tests cover the non-streaming kwargs path, the streaming kwargs path, and the options-dict path. The changes are focused, logically sound, and consistent with the existing codebase patterns.

✓ Security Reliability

This diff is a security improvement that removes the blind forwarding of **kwargs to the underlying Ollama AsyncClient.chat() call, preventing callers from injecting arbitrary, unvalidated parameters into the downstream API. The addition of 'allow_multiple_tool_calls' to exclude_keys and the accompanying regression tests are well-structured. No security or reliability issues are introduced by this change.

✓ Test Coverage

The diff removes forwarding of **kwargs to ollama.AsyncClient.chat() and adds allow_multiple_tool_calls to the exclude_keys set in _prepare_options. Three new tests cover the non-streaming kwargs path, streaming kwargs path, and options-dict path respectively. Coverage is solid for the specific regression case (issue #4402). Minor gaps: no test verifies that arbitrary/unknown kwargs (beyond allow_multiple_tool_calls) are also silently dropped, which would better document the broader behavioral change of removing **kwargs forwarding entirely. test_cmc_ignores_unsupported_options omits a result assertion but the intent is clear.

Suggestions

  • Consider logging a debug-level warning when kwargs are silently dropped, so that users who accidentally pass an unsupported parameter get some feedback during development rather than silent no-op behavior.
  • Consider logging a debug-level warning when kwargs are silently dropped so that callers can diagnose unexpected behaviour during development, rather than having parameters disappear without any signal.
  • Consider adding a test that passes an arbitrary unknown kwarg (e.g., some_future_flag=True) to get_response and asserts it does not appear in the self.client.chat() call. This would document the broader behavioral change (all kwargs are now dropped, not just allow_multiple_tool_calls).
  • In test_cmc_ignores_unsupported_options, consider adding assert result.text == "test" for consistency with the other two new tests, to confirm the response is still valid after filtering.

Automated review by moonbox3's agents

@moonbox3 Evan Mattson (moonbox3) 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.

Reviewed and approved.

@max-montes
Max Montes Soza (max-montes) force-pushed the fix/ollama-filter-unsupported-kwargs branch from 7984fbf to fae0b0e Compare March 3, 2026 19:04
@max-montes
Max Montes Soza (max-montes) force-pushed the fix/ollama-filter-unsupported-kwargs branch from fae0b0e to 6257983 Compare March 3, 2026 19:20
@max-montes

Copy link
Copy Markdown
Contributor Author

Hey Eduard van Valkenburg (@eavanvalkenburg), gentle ping that I made the changes you requested

result = ollama_client.get_response(
messages=chat_history,
stream=True,
allow_multiple_tool_calls=True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is no longer a valid pattern, one should always use options and we have a PR open to remove kwargs in most places(#4581), I think we can close this PR for now

@eavanvalkenburg

Copy link
Copy Markdown
Member

This is superseded by #4581 but thanks for helping out Max Montes Soza (@max-montes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: OllamaChatClient passes unsupported kwargs (e.g. allow_multiple_tool_calls) to ollama.AsyncClient.chat(), causing TypeError

6 participants