Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bugfix: avoid race condition when refreshing google token#2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Kludex merged 2 commits intopydantic:mainfromJadHADDAD92:gemini_token
Jul 15, 2025

Conversation

JadHADDAD92
Copy link
Contributor

Hello,
I have been having an issue with the refresh token when launching multiple Gemini agents concurrently.
Here is a minimal reproducible code:

Class handling agent

classInformationExtractionAgent:def__init__(self):self.model=GeminiModel("gemini-2.5-pro",provider=GoogleVertexProvider(service_account_file=os.path.join("shared","config","gemini.json"),region="europe-west1",            ),        )self.settings=GeminiModelSettings(gemini_thinking_config={"thinking_budget":128,"include_thoughts":True}        )self.agent=Agent(model=self.model,model_settings=self.settings,output_type=DocumentExtractionResult,system_prompt="..."        )asyncdefextract_information(...)->AgentRunResult[DocumentExtractionResult]:result=awaitself.agent.run("...")returnresult

This class is called and used from a coroutine

asyncdefextraction_process(...):information_extraction_agent=InformationExtractionAgent()# [...]extraction_result=awaitinformation_extraction_agent.extract_information(...)# [...]

and this coroutine is called concurrently in a Semaphore

asyncrun_extraction_job(tasks_ids):semaphore=Semaphore(10)# [...]asyncdefsemaphore_execute_task(task_id:int):asyncwithsemaphore:awaitextraction_process(task_id)# [...]awaitasyncio.gather(*[semaphore_execute_task(task_id)fortask_idintask_ids])

I would randomly get this error, not consistently, even though all the instances have the service_account_file configured toshared/config/gemini.json, seems like a race condition:

backend-1  |   File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1654, in _send_handling_authbackend-1  |     request = await auth_flow.__anext__()backend-1  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^backend-1  |   File "/usr/local/lib/python3.11/site-packages/pydantic_ai/providers/google_vertex.py", line 134, in async_auth_flowbackend-1  |     await self._refresh_token()backend-1  |   File "/usr/local/lib/python3.11/site-packages/pydantic_ai/providers/google_vertex.py", line 169, in _refresh_tokenbackend-1  |     assert isinstance(self.credentials.token, str), f'Expected token to be a string, got {self.credentials.token}'  # type: ignore[reportUnknownMemberType]backend-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^backend-1  | AssertionError: Expected token to be a string, got None

with the help of Mert Sırakaya, here is a patch for this error, I have tried running the test many times after the patch, I don't have any errors, and then tried again without the patch and I occasionally got the error again.

For the typing lint I have putignore[reportUnknownMemberType] comments because I didn't know what else to do, I'm open to better suggestions

@DouweM
Copy link
Contributor

@JadHADDAD92 Thanks Jad. This look like the same issue as#1440. There, my colleague Marcelo this should be solved with the newGoogleModel andGoogleProvider(vertexai=True) you can use instead ofGeminiModel andGoogleVertexProvider as in the example here:https://ai.pydantic.dev/models/google/#vertex-ai-enterprisecloud. Can you see if that works?

Note that this PR may still make sense, but I want to verify the new classes also work for you.

@DouweMDouweM self-assigned thisJul 1, 2025
@JadHADDAD92
Copy link
ContributorAuthor

I have triedGoogleModel with the following configs

classInformationExtractionAgent:def__init__(self):credentials=service_account.Credentials.from_service_account_file(os.path.join("shared","config","gemini.json"),scopes=["https://www.googleapis.com/auth/cloud-platform"],        )self.model=GoogleModel("gemini-2.5-pro",provider=GoogleProvider(credentials=credentials,location="europe-west1",            ),        )self.settings=GoogleModelSettings(google_thinking_config={"thinking_budget":128,"include_thoughts":True}        )

And couldn't reproduce my error, so I guess it works!

@DouweM
Copy link
Contributor

@JadHADDAD92 Glad to hear it!

@Kludex What do you think, should we keep fixing issues inGeminiModel or deprecate it in v1 and push people toGoogleModel instead?

@KludexKludexenabled auto-merge (squash)July 15, 2025 10:45
@Kludex
Copy link
Member

We should deprecate this model.

@KludexKludex merged commit46aa248 intopydantic:mainJul 15, 2025
16 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@KludexKludexKludex approved these changes

Assignees

@DouweMDouweM

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@JadHADDAD92@DouweM@Kludex

[8]ページ先頭

©2009-2025 Movatter.jp