Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCP VertexAI: pass explicit scope to google.auth.default() #579

Conversation

jtbaker
Copy link
Contributor

@jtbaker jtbaker commented Dec 31, 2024

Relates to / resolves #575

GCP resolves default application credentials in a specific order documented here: https://cloud.google.com/docs/authentication/application-default-credentials

Search order
ADC searches for credentials in the following locations:
GOOGLE_APPLICATION_CREDENTIALS environment variable
User credentials set up by using the Google Cloud CLI
The attached service account, returned by the metadata server

The existing VertexAI code seems to assume that google.auth.default() returns either a user's credentials or the attached service account (via Workload Identity) and not a service account, which has a different code branching path for auth.

long term seems better and more idiomatic to use google's semantics of GOOGLE_APPLICATION_CREDENTIALS via google.auth.default() itself instead of explicitly passing in the service_account_file parameter/property to the model constructor separately, but that seems like a significant refactor of some of the codebase. This illustrates a possible (inelegant) workaround short term that got my code using a service account for auth working, not necessarily something that should be merged or the long term solution for the library.

Ok, did some more digging, and it seems like we may just need to pass the scopes parameter in to google.auth.default() for the service account case to allow the credentials to refresh their token. Perhaps for gcloud/workload identity cases they are loaded automatically, but for service accounts they need to be more explicit. By removing my changes and adding this, using my service account via google.auth.default() worked. I think that potentially with this change, the service_account_file parameter/property and code could be deprecated in favor of GCP's application default credentials preferred flow.

long term seems better and more idiomatic to use google's semantics of `GOOGLE_APPLICATION_CREDENTIALS` via google.auth.default() instead of explicitly passing in the `service_account_file` parameter/property, but this illustrates a possible (inelegant) workaround
@jtbaker jtbaker changed the title handle case when google.auth.default() uses a service account GCP VertexAI: handle case when google.auth.default() uses a service account Dec 31, 2024
@samuelcolvin samuelcolvin merged commit c53c4e1 into pydantic:main Jan 2, 2025
13 checks passed
@samuelcolvin
Copy link
Member

Works for me!

It's hard to know if this could break auth for someone, the only real way to know is to release this and see if anyone complains.

@samuelcolvin samuelcolvin changed the title GCP VertexAI: handle case when google.auth.default() uses a service account GCP VertexAI: pass explicit scope to google.auth.default() Jan 2, 2025
@jtbaker
Copy link
Contributor Author

jtbaker commented Jan 2, 2025

Thanks @samuelcolvin! What is your release cadence like for this project? I have a POC I'm working on with some Vertex stuff.

@samuelcolvin
Copy link
Member

We'll do a release tomorrow.

@samuelcolvin
Copy link
Member

Done https://github.com/pydantic/pydantic-ai/releases/tag/v0.0.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Google VertexAI Agent OAuth scope Issues
2 participants