Skip to content

Support tool calling when a structured result is provided - #184

Merged
samuelcolvin merged 22 commits into
mainfrom
multiple-tools
Dec 13, 2024
Merged

samuelcolvin merged 22 commits into
mainfrom
multiple-tools

Conversation

@jlowin

@jlowin jlowin commented Dec 8, 2024

Copy link
Copy Markdown
Contributor

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 .

@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

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

Comment thread .github/workflows/ci.yml Outdated
@jlowin
jlowin marked this pull request as ready for review December 9, 2024 21:57

@samuelcolvin samuelcolvin left a comment

Copy link
Copy Markdown
Member

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.

Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
Comment thread .github/workflows/ci.yml Outdated
Comment thread pydantic_ai_slim/pydantic_ai/agent.py Outdated
@jlowin

jlowin commented Dec 10, 2024

Copy link
Copy Markdown
Contributor Author

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

@hyperlint-ai-deprecated hyperlint-ai-deprecated Bot left a comment

Copy link
Copy Markdown
Contributor

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

@hyperlint-ai-deprecated hyperlint-ai-deprecated Bot left a comment

Copy link
Copy Markdown
Contributor

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

jlowin commented Dec 13, 2024

Copy link
Copy Markdown
Contributor Author

@samuelcolvin I think this is all cleaned up now

@samuelcolvin
samuelcolvin merged commit 70105cb into main Dec 13, 2024
@samuelcolvin
samuelcolvin deleted the multiple-tools branch December 13, 2024 17:42
thiagohora pushed a commit to thiagohora/pydantic-ai that referenced this pull request Feb 10, 2026
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
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