Python: moved prepare tools into class (#215)

* moved prepare tools into class

* moved test

* changed tool handling

* fix test

* second fix
This commit is contained in:
Eduard van Valkenburg
2025-07-22 23:37:18 +02:00
committed by GitHub
Unverified
parent 84e5ee97b9
commit 27f7af2160
4 changed files with 80 additions and 79 deletions
@@ -305,3 +305,48 @@ async def test_base_client_with_streaming_function_calling_disabled(chat_client_
updates.append(update)
assert len(updates) == 1
assert exec_counter == 0
def test_chat_options_parsing_tools(chat_client_base, ai_function_tool) -> None:
"""Test that chat options can parse tools correctly."""
def echo() -> str:
"""Echo the input."""
return "Echo"
dict_function = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City and country e.g. Bogotá, Colombia"},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Units the temperature will be returned in.",
},
},
"required": ["location", "units"],
"additionalProperties": False,
},
"strict": True,
},
}
options = ChatOptions(tools=[ai_function_tool, echo, dict_function], tool_choice="auto")
assert len(options.tools) == 3
assert options.tools[0] == ai_function_tool
assert options.tools[1] != echo
assert options.tools[2] == dict_function
# after prepare, the tools should be represented as dicts
# while ai_tools is still the same.
chat_client_base._prepare_tools_and_tool_choice(chat_options=options)
assert options._ai_tools[0] == ai_function_tool
assert options._ai_tools[2] == dict_function
assert len(options.tools) == 3
assert options.tools[0]["function"]["name"] == "simple_function"
assert options.tools[1]["function"]["name"] == "echo"
assert options.tools[2]["function"]["name"] == "get_weather"
-48
View File
@@ -539,54 +539,6 @@ def test_chat_options_and(ai_function_tool, ai_tool) -> None:
assert options3.tools == [ai_function_tool, ai_tool]
def test_chat_options_parsing_tools(ai_function_tool, ai_tool) -> None:
from agent_framework._clients import _prepare_tools_and_tool_choice
def echo() -> str:
"""Echo the input."""
return "Echo"
dict_function = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City and country e.g. Bogotá, Colombia"},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Units the temperature will be returned in.",
},
},
"required": ["location", "units"],
"additionalProperties": False,
},
"strict": True,
},
}
options = ChatOptions(tools=[ai_function_tool, ai_tool, echo, dict_function], tool_choice="auto")
assert len(options.tools) == 4
assert options.tools[0] == ai_function_tool
assert options.tools[1] == ai_tool
assert options.tools[2] != echo
assert options.tools[3] == dict_function
# after prepare, the tools should be represented as dicts
# while ai_tools is still the same.
_prepare_tools_and_tool_choice(options)
assert options._ai_tools[0] == ai_function_tool
assert options._ai_tools[1] == ai_tool
assert options._ai_tools[3] == dict_function
assert len(options.tools) == 4
assert options.tools[0]["function"]["name"] == "simple_function"
assert options.tools[1]["function"]["name"] == "generic_tool"
assert options.tools[2]["function"]["name"] == "echo"
assert options.tools[3]["function"]["name"] == "get_weather"
# region Agent Response Fixtures