Skip to content

Commit

Permalink
Validate anthropic_tool function args as json (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodbridge authored Jul 6, 2024
1 parent 3b8988e commit ba66784
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 142 in instructor/function_calls.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)

Check failure on line 142 in instructor/function_calls.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)

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(
Expand Down

0 comments on commit ba66784

Please sign in to comment.