From ba667844ac04f3a0ade1649ec7467a83488132fa Mon Sep 17 00:00:00 2001 From: Justin Woodbridge Date: Sat, 6 Jul 2024 13:24:12 -0400 Subject: [PATCH] Validate anthropic_tool function args as json (#779) --- instructor/function_calls.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/instructor/function_calls.py b/instructor/function_calls.py index ed0230db2..3972c1644 100644 --- a/instructor/function_calls.py +++ b/instructor/function_calls.py @@ -136,14 +136,19 @@ def parse_anthropic_tools( validation_context: Optional[dict[str, Any]] = None, strict: Optional[bool] = None, ) -> BaseModel: - tool_calls = [c.input for c in completion.content if c.type == "tool_use"] # type: ignore - TODO update with anthropic specific types + # Anthropic returns arguments as a dict, dump to json for model validation below + tool_calls = [ + json.dumps(c.input) for c in completion.content if c.type == "tool_use" + ] # type: ignore - TODO update with anthropic specific types tool_calls_validator = TypeAdapter( Annotated[list[Any], Field(min_length=1, max_length=1)] ) tool_call = tool_calls_validator.validate_python(tool_calls)[0] - return cls.model_validate(tool_call, context=validation_context, strict=strict) + return cls.model_validate_json( + tool_call, context=validation_context, strict=strict + ) @classmethod def parse_anthropic_json(