-
Notifications
You must be signed in to change notification settings - Fork 171
feat(pydentic_ai): tools & multiple input/output messages support #2508
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
Open
satyadevai
wants to merge
5
commits into
Arize-ai:main
Choose a base branch
from
satyadevai:2476-pydentic-tool-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7999272
exploring pydentic instrumentation
satyadevai 728445f
Input Messsages & Output Messages Fixx
satyadevai 43fbf5c
Fixing Multiple Messages & Tools Results, Tool Inputs, Status code on…
satyadevai 6b2e632
Fixing Unit test cases
satyadevai 284e0f0
Cursor Review comments
satyadevai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...on/instrumentation/openinference-instrumentation-pydantic-ai/examples/multi_tool_agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.sdk import trace as trace_sdk | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
| from pydantic_ai import Agent, RunContext | ||
|
|
||
| from openinference.instrumentation.pydantic_ai import OpenInferenceSpanProcessor | ||
|
|
||
| # OpenTelemetry setup | ||
| endpoint = "http://localhost:6006/v1/traces" | ||
| tracer_provider = trace_sdk.TracerProvider() | ||
| exporter = OTLPSpanExporter(endpoint=endpoint) | ||
| trace.set_tracer_provider(tracer_provider) | ||
| tracer_provider.add_span_processor(OpenInferenceSpanProcessor()) | ||
| tracer_provider.add_span_processor(BatchSpanProcessor(exporter)) | ||
|
|
||
|
|
||
| # Simple dependencies | ||
| class Deps: | ||
| def __init__(self, user_id: str): | ||
| self.user_id = user_id | ||
|
|
||
|
|
||
| # Create agent that will use multiple tools | ||
| agent = Agent( | ||
| "openai:gpt-4.1-nano", | ||
| deps_type=Deps, | ||
| system_prompt="You are a helpful assistant. Use available tools to gather information.", | ||
| instrument=True, | ||
| ) | ||
|
|
||
|
|
||
| @agent.tool | ||
| def get_weather(ctx: RunContext[Deps], city: str) -> str: | ||
| """Get weather information for a city.""" | ||
| print(f"[TOOL 1] Getting weather for: {city}") | ||
| weather_data = { | ||
| "San Francisco": "Sunny, 72°F", | ||
| "New York": "Cloudy, 65°F", | ||
| "Seattle": "Rainy, 58°F", | ||
| } | ||
| return weather_data.get(city, f"Weather data not available for {city}") | ||
|
|
||
|
|
||
| @agent.tool | ||
| def get_time(ctx: RunContext[Deps], city: str) -> str: | ||
| """Get current time for a city.""" | ||
| print(f"[TOOL 2] Getting time for: {city}") | ||
| time_data = { | ||
| "San Francisco": "10:30 AM PST", | ||
| "New York": "1:30 PM EST", | ||
| "Seattle": "10:30 AM PST", | ||
| } | ||
| return time_data.get(city, f"Time data not available for {city}") | ||
|
|
||
|
|
||
| def main(): | ||
| deps = Deps(user_id="user_123") | ||
| result = agent.run_sync("What's the weather and time in San Francisco?", deps=deps) | ||
| print("AGENT RESPONSE:") | ||
| print(result.output) | ||
| for i, msg in enumerate(result.all_messages(), 1): | ||
| print(f"\nStep {i}: {msg}") | ||
| if hasattr(msg, "parts"): | ||
| for part in msg.parts: | ||
| if part.part_kind == "tool-call": | ||
| print(f" → Tool Call: {part.tool_name}({part.args})") | ||
| elif part.part_kind == "tool-return": | ||
| print(f" ← Tool Result: {part.content}") | ||
| elif part.part_kind == "text": | ||
| print(f" 💬 Text: {part.content[:100]}...") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.