Commit 62e05e3
feat(genai): add labels support (#1503)
<!--
# Thank you for contributing to LangChain-google!
-->
<!--
## Checklist for PR Creation
- [x] PR Title: "<type>[optional scope]: <description>"
- Where type is one of: feat, fix, docs, style, refactor, perf, test,
build, ci, chore, revert, release
- Scope is used to specifiy the package targeted. Options are: genai,
vertex, community, infra (repo-level)
- [x] PR Description and Relevant issues:
- Description of the change
- Relevant issues (if applicable)
- Any dependencies required for this change
- [x] Add Tests and Docs:
- If adding a new integration:
1. Include a test for the integration (preferably unit tests that do not
rely on network access)
2. Add an example notebook showing its use (place in the
`docs/docs/integrations` directory)
- [x] Lint and Test:
- Run `make format`, `make lint`, and `make test` from the root of the
package(s) you've modified
- See contribution guidelines for more:
https://github.com/langchain-ai/langchain-google/blob/main/README.md#contribute-code
-->
<!--
## Additional guidelines
- [x] PR title and description are appropriate
- [x] Necessary tests and documentation have been added
- [x] Lint and tests pass successfully
- [x] The following additional guidelines are adhered to:
- Optional dependencies are imported within functions
- No unnecessary dependencies added to pyproject.toml files (except
those required for unit tests)
- PR doesn't touch more than one package
- Changes are backwards compatible
-->
## Description
<!-- e.g. "Implement user authentication feature" -->
Added the `labels` field to `ChatGoogleGenerativeAI`. This allows users
to attach custom metadata to API requests and track billing breakdowns.
This feature was already implemented in `ChatVertexAI`, and this change
makes it available in `ChatGoogleGenerativeAI` as well.
## Relevant issues
<!-- e.g. "Fixes #000" -->
fixes #1496
## Type
<!-- Select the type of Pull Request -->
<!-- Keep only the necessary ones -->
🆕 New Feature
## Changes(optional)
<!-- List of changes -->
- Added `labels: dict[str, str] | None` field to
`_BaseGoogleGenerativeAI`
- Implemented logic to pass `labels` to `GenerateContentConfig` in
`ChatGoogleGenerativeAI`
- Added unit tests to verify that labels is passed correctly
## Testing(optional)
<!-- Test procedure -->
```python
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
labels={"env": "production", "team": "ml"},
)
```
<!-- Test result -->
Unit tests added:
- `labels` is correctly passed to `GenerateContentConfig`
- `labels` defaults to None when not specified
## Note(optional)
<!-- Information about the errors fixed by PR -->
<!-- Remaining issue or something -->
<!-- Other information about PR -->
`ChatVertexAI` includes labels validation [^1], but this was omitted in
`ChatGoogleGenerativeAI` to align with other fields that do not have
validation.
[^1]:
https://github.com/langchain-ai/langchain-google/blob/306f6ee3d8d03672d95f8907edefa61e41bf02d3/libs/vertexai/langchain_google_vertexai/chat_models.py#L1858-L1868
Google GenAI SDK already supports the `labels` field at here.
https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig.labels
---------
Co-authored-by: Mason Daugherty <mason@langchain.dev>
Co-authored-by: Mason Daugherty <github@mdrxy.com>1 parent 474b8fb commit 62e05e3
3 files changed
Lines changed: 65 additions & 0 deletions
File tree
- libs/genai
- langchain_google_genai
- tests/unit_tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
506 | 506 | | |
507 | 507 | | |
508 | 508 | | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
509 | 519 | | |
510 | 520 | | |
511 | 521 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2797 | 2797 | | |
2798 | 2798 | | |
2799 | 2799 | | |
| 2800 | + | |
| 2801 | + | |
| 2802 | + | |
| 2803 | + | |
2800 | 2804 | | |
2801 | 2805 | | |
2802 | 2806 | | |
| |||
2824 | 2828 | | |
2825 | 2829 | | |
2826 | 2830 | | |
| 2831 | + | |
2827 | 2832 | | |
2828 | 2833 | | |
2829 | 2834 | | |
| |||
2971 | 2976 | | |
2972 | 2977 | | |
2973 | 2978 | | |
| 2979 | + | |
2974 | 2980 | | |
2975 | 2981 | | |
2976 | 2982 | | |
| |||
3001 | 3007 | | |
3002 | 3008 | | |
3003 | 3009 | | |
| 3010 | + | |
3004 | 3011 | | |
3005 | 3012 | | |
3006 | 3013 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4921 | 4921 | | |
4922 | 4922 | | |
4923 | 4923 | | |
| 4924 | + | |
| 4925 | + | |
| 4926 | + | |
| 4927 | + | |
| 4928 | + | |
| 4929 | + | |
| 4930 | + | |
| 4931 | + | |
| 4932 | + | |
| 4933 | + | |
| 4934 | + | |
| 4935 | + | |
| 4936 | + | |
| 4937 | + | |
| 4938 | + | |
| 4939 | + | |
| 4940 | + | |
| 4941 | + | |
| 4942 | + | |
| 4943 | + | |
| 4944 | + | |
| 4945 | + | |
| 4946 | + | |
| 4947 | + | |
| 4948 | + | |
| 4949 | + | |
| 4950 | + | |
| 4951 | + | |
| 4952 | + | |
| 4953 | + | |
| 4954 | + | |
| 4955 | + | |
| 4956 | + | |
| 4957 | + | |
| 4958 | + | |
| 4959 | + | |
| 4960 | + | |
| 4961 | + | |
| 4962 | + | |
| 4963 | + | |
| 4964 | + | |
| 4965 | + | |
| 4966 | + | |
| 4967 | + | |
| 4968 | + | |
| 4969 | + | |
| 4970 | + | |
| 4971 | + | |
0 commit comments