Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Created March 12, 2025 10:26
Show Gist options
  • Save samuelcolvin/ca64bbb752cfcc46dcec27e264b69888 to your computer and use it in GitHub Desktop.
Save samuelcolvin/ca64bbb752cfcc46dcec27e264b69888 to your computer and use it in GitHub Desktop.
instrumenting openai agents with logfire and capturing exception tracebacks in function calls
from typing_extensions import TypedDict
import logfire
from httpx import AsyncClient
from agents import RunContextWrapper, Agent, function_tool, Runner
from agents.tool import WebSearchTool
logfire.configure()
logfire.instrument_openai_agents()
class Location(TypedDict):
lat: float
long: float
@function_tool(failure_error_function=None)
async def fetch_weather(ctx: RunContextWrapper[AsyncClient], location: Location) -> str:
"""Fetch the weather for a given location.
Args:
ctx: Run context object.
location: The location to fetch the weather for.
"""
1 / 0
return 'sunny'
agent = Agent(
name='weather agent',
tools=[fetch_weather, WebSearchTool()]
)
async def main():
async with AsyncClient() as client:
logfire.instrument_httpx(client)
result = await Runner.run(agent, "What's the weather link in London", context=client)
print(result.final_output)
if __name__ == '__main__':
import asyncio
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment