Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Created March 12, 2025 10:26

Revisions

  1. samuelcolvin created this gist Mar 12, 2025.
    44 changes: 44 additions & 0 deletions logfire_instrument_openai_agents.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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())