Created
July 25, 2025 00:01
-
-
Save bbrowning/25b19518cb44748c42adcc234b782e07 to your computer and use it in GitHub Desktop.
An example of how to use Pydantic AI with Llama Stack and the Responses API
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
| # Dependencies: | |
| # pip install openai pydantic-ai | |
| # This example uses the web_search builtin tool, so it assumes you | |
| # have a valid TAVILY_API_KEY environment variable set before starting | |
| # your Llama Stack server. | |
| # Usage: | |
| # | |
| # ollama run llama3.2:3b | |
| # | |
| # ENABLE_OLLAMA="ollama" OLLAMA_INFERENCE_MODEL="llama3.2:3b" uv run llama stack run llama_stack/templates/starter/run.yaml | |
| # | |
| # python pydantic_agent_test.py | |
| from openai import AsyncOpenAI | |
| from openai.types.responses import WebSearchToolParam | |
| from pydantic import BaseModel | |
| from pydantic_ai import Agent | |
| from pydantic_ai.models.openai import OpenAIResponsesModel, OpenAIResponsesModelSettings | |
| from pydantic_ai.providers.openai import OpenAIProvider | |
| client = AsyncOpenAI( | |
| base_url="http://localhost:8321/v1/openai/v1", | |
| api_key="fake", | |
| ) | |
| model_settings = OpenAIResponsesModelSettings( | |
| openai_builtin_tools=[WebSearchToolParam(type="web_search_preview")], | |
| ) | |
| model = OpenAIResponsesModel( | |
| "ollama/llama3.2:3b", | |
| provider=OpenAIProvider(openai_client=client), | |
| ) | |
| agent = Agent(model, model_settings=model_settings) | |
| result = agent.run_sync('What is the weather in Tokyo?') | |
| print(result.output) | |
| #> The current weather in Tokyo is sunny with a temperature of 32.2°C (90°F). The wind is blowing at 3.6 mph from the southwest, and the pressure is 1013.0 mb. There is no precipitation expected, but the humidity is relatively high at 63%. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment