Non string responses from function tools #2575
-
|
The code samples, I saw so far about registering self-defined (as python function) tools in agents, the return type of the function was always str (see here for example). Does this mean, that at the moment only str output is officially supported? Because the docs do not say something about arbitrary response types. However, I learned from reading source code, that I can return pydantic models for instance, when implementing a to_dict method like here: class TestAIFunction:
async def test_complex_ai_tool_response(self):
from agent_framework import ai_function, prepare_function_call_results
from pydantic import BaseModel
class Box(BaseModel):
height: int
width: int
def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True) -> dict[str, Any]:
return self.model_dump(
by_alias=True,
exclude=exclude,
exclude_none=exclude_none,
)
def get_box(height: int, width: int) -> Box:
"""Get a box with a defined height and with."""
return Box(height=height, width=width)
tool = ai_function(get_box, description=get_box.__doc__ or "")
tool_call_result = await tool.invoke(height=10, width=20)
assert prepare_function_call_results(tool_call_result) == "{\"height\": 10, \"width\": 20}"The crux here is the prepare_function_call_results function, that converts tool call execution results into strings. Is this the recommended way? Or is there simply no proper solution for that at the moment? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Wouldn't it make sense to get a string by using the model_dump_json() on BaseModel instances here? def prepare_function_call_results(content: Contents | Any | list[Contents | Any]) -> str:
"""Prepare the values of the function call results."""
if isinstance(content, Contents):
# For BaseContent objects, use to_dict and serialize to JSON
return json.dumps(content.to_dict(exclude={"raw_representation", "additional_properties"}))
elif isinstance(content, BaseModel):
return content.model_dump_json()
dumpable = _prepare_function_call_results_as_dumpable(content)
if isinstance(dumpable, str):
return dumpable
# fallback
return json.dumps(dumpable) |
Beta Was this translation helpful? Give feedback.
-
|
Ah, I saw in the latest release notes it was done meanwhile, see #2606 |
Beta Was this translation helpful? Give feedback.
Ah, I saw in the latest release notes it was done meanwhile, see #2606