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

Support tool calling when a structured result is provided #184

Merged
merged 22 commits into from
Dec 13, 2024

Conversation

jlowin
Copy link
Collaborator

@jlowin jlowin commented Dec 8, 2024

This PR addresses behavior when an agent requests multiple tool calls, one of which is a "final" result call. I believe it closes #161 and follows #179.

In this PR:

  • agents have a new end_strategy parameter that can be either "early" or "correct". Early means that the presence of a "final" result short-circuits all other tool calls, and they are not made. Passing "correct" will call all tools, even if a final result was provided. correct is the default value.
  • no matter what the end_strategy is, a ToolResult message is always generated for every tool call. This ensures that messages from the agent call can safely be used as the input to another call.
  • Ruff (correctly) claimed that the _handle_model_response method was very complicated, so I split it into a few straightforward method calls. If that's not desirable, it's easy to recombine.

Note: this PR does not address the streaming case, as that is more complexing pending conversation with @samuelcolvin .

Copy link

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

Deploying pydantic-ai with  Cloudflare Pages  Cloudflare Pages

Latest commit: d3a01f1
Status: ✅  Deploy successful!
Preview URL: https://08121d92.pydantic-ai.pages.dev
Branch Preview URL: https://multiple-tools.pydantic-ai.pages.dev

View logs

.github/workflows/ci.yml Outdated Show resolved Hide resolved
@jlowin jlowin marked this pull request as ready for review December 9, 2024 21:57
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 this looks great.

I'm happy to help or take over if my feedback is too annoying.

pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
.github/workflows/ci.yml Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
@jlowin
Copy link
Collaborator Author

jlowin commented Dec 10, 2024

All good feedback. Will clean up today or tomorrow as time allows.

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

The style guide flagged several spelling errors that seemed like false positives. We skipped posting inline suggestions for the following words:

  • [Ss]how_root_heading

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

The style guide flagged several spelling errors that seemed like false positives. We skipped posting inline suggestions for the following words:

  • show_root_heading

@jlowin
Copy link
Collaborator Author

jlowin commented Dec 13, 2024

@samuelcolvin I think this is all cleaned up now

@samuelcolvin samuelcolvin merged commit 70105cb into main Dec 13, 2024
16 checks passed
@samuelcolvin samuelcolvin deleted the multiple-tools branch December 13, 2024 17:42
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.

OpenAI BadRequest with multiple "final_result" tool calls and malformed generation
2 participants