-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed as not planned
Closed as not planned
Copy link
Labels
questionFurther information is requestedFurther information is requested
Description
OpenAI API Timeout Error in pydantic-ai Agent
Problem Description
When using the OpenAI model with the pydantic-ai Agent, a persistent APITimeoutError is encountered during API requests, despite having a valid and current API key.
Environment
- Library: pydantic-ai
- Model Attempted: OpenAI (gpt-4o)
- API Key Status: Valid and current
Code
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.models.anthropic import AnthropicModel
from dotenv import load_dotenv
import os
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
agent = Agent(
# model = OpenAIModel('gpt-4o', api_key=OPENAI_API_KEY)
# model = GeminiModel('gemini-1.5-flash', api_key=GEMINI_API_KEY)
model = AnthropicModel('claude-3-5-sonnet-latest', api_key=ANTHROPIC_API_KEY)
)
result = agent.run_sync("What color is an apple?")
print(result.data)
Error Traceback
openai.APITimeoutError: Request timed out.
Observations
- This error occurs specifically with the OpenAI model
- Requests using Gemini and Anthropic models work without issues
Additional Context
I've verified that a direct OpenAI API call using the openai library works correctly, suggesting the issue may be specific to the pydantic-ai library's implementation of the OpenAI model. The following code works fine:
import os
from openai import OpenAI
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from typing import Literal
class MyModel(BaseModel):
place_of_interest: str = Field(description = "The most popular museum in the city")
nearest_water_body: str = Field(description = "The nearest lake, or a large natural freshwater source")
country: str = Field(description = "The country to which the city belongs")
situated: Literal["Northern Hemisphere", "Southern Hemisphere"]
famous_person_from_city: str = Field(description = "The most popular political figure from the place, ensuring they were born there")
why_famous: str = Field(description = "Why the most popular political figure")
load_dotenv()
client = OpenAI(api_key = os.getenv("OPENAI_API_KEY1"))
def get_response(query: str):
response = client.beta.chat.completions.parse(
# query=query,
model="gpt-4o-mini",
messages=[{"role": "system","content": "You know all about world geography, and especially about cities! You respond in the tone of a pirate!"},
{"role": "user","content": query}],
temperature=0.2,
response_format=MyModel,
top_p=1
)
return response
Reproducibility
Consistently reproducible locally
lmaddox
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested