Non string responses from function tools #2575
Unanswered
sebastian-velten
asked this question in
Q&A
Replies: 1 comment
-
|
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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:
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.
All reactions