Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make args handling more robust #489

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Dec 18, 2024

I believe this should close #398. It would be good if @sadransh could confirm this though.

I think what was happening there was that the API response was sending data in an unexpected format (already a dict rather than just a JSON string), and our implementation of OpenAIModel assumed it would be a string, and didn't check that at runtime.

In practice, it seems like a likely mistake to be made by LLM implementers to claim to be api-compatible with a model, but have the tool data returned as a string vs. as an object, so it feels reasonable to me to be more defensive about the object we get back. In this PR, I removed the from_json and from_dict methods on ToolCallPart and replaced it with one from_raw_args that always checks the type and produces the appropriate kind of Args object.

I also added methods to get the args object as a json string or as a dict — while you'd expect most models to do the right thing internally, it's feasible that a user might generate some messages with one model and then run further requests to another model, but reusing the original messages. If the implementation of the other model expects the tool calls to be in a different format, we'll unnecessarily generate errors when we could just convert the data from dict to json or vice versa at runtime.

The overhead of checking the type of the tool call data when parsing or generating vendor-compatible messages seems low enough to be negligible in the grand scheme, so I think the improved handling of mis-typed responses is more beneficial than harmful.

@dmontagu dmontagu force-pushed the dmontagu/make-args-handling-more-robust branch from a661fd0 to 1a8ce95 Compare December 19, 2024 00:10
Copy link

cloudflare-workers-and-pages bot commented Dec 19, 2024

Deploying pydantic-ai with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1a8ce95
Status: ✅  Deploy successful!
Preview URL: https://a7dc46dd.pydantic-ai.pages.dev
Branch Preview URL: https://dmontagu-make-args-handling.pydantic-ai.pages.dev

View logs

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM.

@@ -190,12 +190,34 @@ class ToolCallPart:
"""Part type identifier, this is available on all parts as a discriminator."""

@classmethod
def from_json(cls, tool_name: str, args_json: str, tool_call_id: str | None = None) -> Self:
return cls(tool_name, ArgsJson(args_json), tool_call_id)
def from_raw_args(cls, tool_name: str, args: str | dict[str, Any], tool_call_id: str | None = None) -> Self:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be just from_args to be slightly more concise?

@samuelcolvin samuelcolvin merged commit a2241c2 into main Dec 19, 2024
16 checks passed
@samuelcolvin samuelcolvin deleted the dmontagu/make-args-handling-more-robust branch December 19, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

text-generation-inference (TGI) support
2 participants