-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Docs: add an example of using RunContext to pass data among tools #2316
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
Conversation
PR Change SummaryAdded an example demonstrating how to use RunContext for passing data among tools, particularly in data analytics workflows.
Modified Files
Added Files
How can I customize these reviews?Check out the Hyperlint AI Reviewer docs for more information on how to customize the review. If you just want to ignore it on this PR, you can add the Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add |
2533e75
to
004db97
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonyxwz Thanks Tony! Just a few suggestions
f1ddfda
to
c65c67a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed a new revision.
@@ -13,6 +13,7 @@ In this example, we'll build an agent that analyzes the [Rotten Tomatoes movie r | |||
|
|||
|
|||
Demonstrates: | |||
|
|||
- [agent dependencies](../dependencies.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/pydantic/pydantic-ai/actions/runs/16605150201/job/46974951928#step:9:46
Run uv run --no-project --with httpx .github/set_docs_pr_preview_url.py Installed 8 packages in 5ms Docs preview URL: https://91de4635-pydantic-ai-previews.pydantic.workers.dev/ Pull request number not set Error: Process completed with exit code 1.
dataset: reference string to the DataFrame | ||
sql: the query to be executed using DuckDB | ||
""" | ||
data = ctx.deps.output[dataset] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use get
here right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed to using get.
|
||
|
||
if __name__ == '__main__': | ||
deps = AnalystAgentDeps(output={}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make this a bit cleaner by giving the output
field a default value of {}
, and then passing deps=AnalystAgentDeps()
below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added field and default_factory in the class
|
||
@analyst_agent.tool | ||
def display(ctx: RunContext[AnalystAgentDeps], name: str) -> str: | ||
"""Display at most 5 rows of the dataframe .""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Display at most 5 rows of the dataframe .""" | |
"""Display at most 5 rows of the dataframe.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonyxwz Thanks Tony, this'll be a nice example -- just a few final suggestions and I'll merge
user_prompt='Count how many negative comments are there in the dataset `cornell-movie-review-data/rotten_tomatoes`', | ||
deps=deps, | ||
) | ||
debug(result.output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just use print
here. Don't forget to update the output in the doc as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
19486f9
to
5ee12e0
Compare
Suggested by DouweM Co-authored-by: Douwe Maan <[email protected]>
9a83451
to
ae9943e
Compare
@tonyxwz Thanks Tony, nice addition! |
I find it a very common pattern in my day job to share large output with another tool (API output, dataset etc). The pattern can be implemented by
There is no such examples in the documentation to illustrate this usage so I added one. Hope it will be useful to other users.
The format of the variable reference in the example is inspired by IPython:
Out[1]
.I am more familiar with using Anthropic models on AWS, so I am actually using Claude 3.7 in my local testing. But the example should work with OpenAI models too.
Checks
make
Running the Example