Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
Description
I'm running Gemini 1.5 with VertexAI provider in the Cloud (GCP) and after some succcesful requests I get 401 for some time with response:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_EXPIRED",
"domain": "googleapis.com",
"metadata": {
"service": "aiplatform.googleapis.com",
"method": "google.cloud.aiplatform.v1.PredictionService.GenerateContent"
}
}
]
}
}
I see in the code that PydanticAI uses itw own attribute token_created
to manage tokens and refreshes them after 3000s.
However, my access token is valid for 1800s only (even though GCP docs say that access tokens last 3600s...). This means that for the 20 minutes after that, all requests fail, given that the token is expired but PydanticAI doesn't know about it and so doesn't refresh it.
Wondering what was the reason for using token_created
, instead of one of the properties valid
or expired
provided by the google-auth
library to decide when to refresh?
P.s.: My service account already has the correct role to access Gemini in the Cloud.
Thank you 🙏
Example Code
# This is just a part of the code and it's not designed to do anything meaningful
# I'm actually using the async version in the Cloud
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
from pydantic_ai.settings import ModelSettings
provider = GoogleVertexProvider(project_id="...", region="...")
gemini_15 = GeminiModel("gemini-1.5-pro", provider=provider)
agent = Agent(
gemini_15,
result_type=str,
model_settings=ModelSettings(temperature=0.0, max_tokens=50)
)
result = agent.run_sync("Return 1 random character from the latin alphabet.")
Python, Pydantic AI & LLM client version
python 3.12.9
pydantic-ai-slim 0.0.42
Gemini 1.5 Pro