Python: Fix ollama_chat_client.py sample: pass tools via options dict#6480
Python: Fix ollama_chat_client.py sample: pass tools via options dict#6480giles17 wants to merge 2 commits into
Conversation
The sample was passing tools as a direct keyword argument to get_response(), which caused a TypeError. The tools parameter must be passed inside the options dict per the SupportsChatGetResponse protocol. Fixes microsoft#6411 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes the Ollama Python sample to comply with the SupportsChatGetResponse/FunctionInvocationLayer.get_response API shape by passing tool definitions via the options parameter (instead of an unsupported tools= keyword argument), eliminating the runtime TypeError reported in #6411.
Changes:
- Update the streaming
get_response()call to pass tools viaoptions={"tools": get_time}. - Update the non-streaming
get_response()call to pass tools viaoptions={"tools": get_time}.
There was a problem hiding this comment.
Automated Code Review
Reviewers: 5 | Confidence: 95%
✓ Correctness
The fix is correct. The
get_responseprotocol signature (lines 171-180 of_clients.py) acceptsoptionsas a typed parameter but has notoolskeyword argument. The_ChatOptionsBaseTypedDict (line 3393 of_types.py) declarestools: ToolTypes | Callable[.., Any] | Sequence[ToolTypes | Callable[..., Any]] | None, confirming that{"tools": get_time}is the correct way to pass tools via the options dict. The single callableget_timematches the accepted type.
✓ Security Reliability
This is a straightforward and correct bug fix to a sample file. The
get_response()protocol method acceptsoptionsas a keyword-only parameter (a TypedDict), nottoolsdirectly. The fix correctly moves thetoolsvalue into theoptionsdict, matching theSupportsChatGetResponseprotocol signature at_clients.py:176and the_ChatOptionsBaseTypedDict definition at_types.py:393. No security or reliability concerns — this is a sample file passing a locally-defined function reference through a well-typed options dict.
✓ Test Coverage
This is a correct sample fix that aligns the code with the SupportsChatGetResponse protocol. The underlying functionality (passing tools via options dict) is already well-tested in test_ollama_chat_client.py with tests like test_cmc_streaming_with_tool_call, test_cmc_integration_with_tool_call, and test_cmc_streaming_integration_with_tool_call. The single-tool (non-list) pattern used in the sample is valid because validate_tools in the core package normalizes single tools into lists. As a sample file fix, no additional automated tests are needed.
✓ Failure Modes
The change correctly fixes the sample by passing
toolsinside theoptionsdict, matching theSupportsChatGetResponseprotocol signature. The protocol'sget_responsemethod acceptsoptions: OptionsContraT | ChatOptions[Any] | None(line 176 of _clients.py), andChatOptions(a TypedDict) defines atoolsfield (line 3393 of _types.py). No failure modes are introduced.
✗ Design Approach
The PR fixes the wrong keyword argument, but it still leaves the Ollama sample on a broken tool-calling path by passing a single
FunctionToolwhere the Ollama client expects a list of tools. I found one blocking design issue in the changed sample lines.
Automated review by giles17's agents
_prepare_tools_for_ollama iterates the tools value, so it must be a list rather than a bare FunctionTool instance. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Description
Fixes the
ollama_chat_client.pysample which was passingtoolsas a direct keyword argument toget_response(), causing aTypeError. Thetoolsparameter must be passed inside theoptionsdict per theSupportsChatGetResponseprotocol.Changes
python/samples/02-agents/providers/ollama/ollama_chat_client.py: Changedtools=get_timetooptions={"tools": get_time}in both the streaming and non-streamingget_response()calls.Testing
Ran the sample locally with Ollama (mistral model) - no TypeError, runs successfully.
Fixes #6411