Skip to content

Support field labels for GeminiModel and GoogleModel on Vertex AI #1056

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

Merged

Conversation

vricciardulli
Copy link
Contributor

Gemini requests on Vertex AI support custom metadata labels. For example, one might use the labels to know which calls are coming from which projects and thus accurately keep track of billing.

See the Gemini API docs for more info.

@vricciardulli vricciardulli marked this pull request as draft March 5, 2025 13:14
@Kludex
Copy link
Member

Kludex commented Mar 5, 2025

Do other providers support labels? Do you know?

Copy link
Contributor Author

@vricciardulli vricciardulli Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: Not sure if this is ok, but I've created a new file for this test because:

  • This test doesn't go in tests/models/test_gemini.py, as import of google.auth might be unsuccessful, which would cause skipping the whole file.

@vricciardulli
Copy link
Contributor Author

vricciardulli commented Mar 5, 2025

Do other providers support labels? Do you know?

@Kludex I don't think so, but I'm not 100% sure. From my experience, usually api-key-based providers use different api keys to track usage and spending. For example, with OpenAI you can create different projects and each project has its own key and that's how you track usage and spending across projects.

@vricciardulli vricciardulli marked this pull request as ready for review March 5, 2025 14:23
@Kludex
Copy link
Member

Kludex commented Mar 13, 2025

I'll check this tomorrow. Sorry.

@vricciardulli
Copy link
Contributor Author

I'll check this tomorrow. Sorry.

no worries, take your time!

@vricciardulli vricciardulli changed the title Support field labels for Gemini models Support field labels for Gemini models on Vertex AI Mar 15, 2025
@vricciardulli
Copy link
Contributor Author

@Kludex sorry to bother, do you think someone has time to check this soon?

Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I forgot to press "comment".

Comment on lines 1 to 2
# pyright: reportDeprecated=false
# pyright: reportPrivateUsage=false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those shouldn't be necessary in the top of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed # pyright: reportDeprecated=false

I also removed # pyright: reportPrivateUsage=false but then I manually built the mock response content. Just FYI, because in other testing modules it's not like that.

request_data['safety_settings'] = gemini_safety_settings

if gemini_labels := model_settings.get('gemini_labels'):
provider_name = self._provider.name if isinstance(self._provider, Provider) else self._provider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an error on Gemini if send this parameter? It would be cool to be provider-agnostic.

This is just a comment. I'm thinking...

Copy link
Contributor Author

@vricciardulli vricciardulli Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we get a 400. Tested with Gemini-1.5-flash (GLA) raises:

pydantic_ai.exceptions.ModelHTTPError: status_code: 400, model_name: gemini-1.5-flash

with body:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"labels\": Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"labels\": Cannot find field."
          }
        ]
      }
    ]
  }
}

Could also just let the user deal with this and then no need to check the provider.

@DouweM
Copy link
Contributor

DouweM commented Apr 30, 2025

We're redoing the Google provider at #1373, so related PRs are on hold until that lands.

@DouweM DouweM marked this pull request as draft April 30, 2025 21:47
@Kludex
Copy link
Member

Kludex commented May 23, 2025

I'll check this in a bit.

Ping me when pipeline is passing please. 👍

@vricciardulli
Copy link
Contributor Author

vricciardulli commented May 23, 2025

@DouweM or @Kludex If possible, I'd need some guidance here, please: I've implemented the labels for both GeminiModel and GoogleModel.

However, with GLA they behave differently. If a user provides labels in the settings dict:

  • GeminiModel: Ignores the labels and moves on with the request to Gemini.
  • GoogleModel: Raises ValueError before any requet is made (this is just what the google-genai library does).

I'm thinking it'd be best if they both behaved the same way, so which would be the best approach in your opinion?

As for the tests, the failing tests are VertexAI-related, but I can't figure out why the CI can't match the requests recorded in the cassettes:

EDIT: managed to fix the tests

@vricciardulli vricciardulli marked this pull request as ready for review May 23, 2025 09:50
@vricciardulli
Copy link
Contributor Author

I'll check this in a bit.

Ping me when pipeline is passing please. 👍

@Kludex tests are passing now:) I've got a question #1056 (comment)

@vricciardulli vricciardulli changed the title Support field labels for Gemini models on Vertex AI Support field labels for GeminiModel and GoogleModel on Vertex AI May 23, 2025
Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks amazing. Sorry for the long wait.

I think the difference in behavior is fine.

@Kludex Kludex merged commit c8bad12 into pydantic:main May 26, 2025
18 checks passed
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.

3 participants