Skip to content

test(genai): make grounding_metadata assertions resilient to schema additions#1806

Open
obchain (obchain) wants to merge 1 commit into
langchain-ai:mainfrom
obchain:fix/genai-grounding-test-version-resilient
Open

test(genai): make grounding_metadata assertions resilient to schema additions#1806
obchain (obchain) wants to merge 1 commit into
langchain-ai:mainfrom
obchain:fix/genai-grounding-test-version-resilient

Conversation

@obchain

Copy link
Copy Markdown

Description

tests/unit_tests/test_chat_models.py::test_response_to_result_grounding_metadata started failing on google-genai >= 1.69.0 because that release added GroundingSupport.rendered_parts. The field defaults to None, so GenerateContentResponse.model_validate(...).model_dump() now emits "rendered_parts": None inside grounding_supports, which the frozen expected_grounding_metadata dict in the test did not contain.

Reproduced locally on google-genai==1.74.0:

FAILED tests/unit_tests/test_chat_models.py::test_response_to_result_grounding_metadata[raw_response0-expected_grounding_metadata0]
FAILED tests/unit_tests/test_chat_models.py::test_response_to_result_grounding_metadata[raw_response2-expected_grounding_metadata2]

The conversion code itself is correct — the test was the brittle part. Rather than freezing the expected dict to one specific upstream version (and breaking the older end of the supported range, or re-breaking on the next field addition), recursively strip None values from both sides before comparing. The assertion still fails on any mismatched or missing real value the test cares about; it only ignores newly added optional fields that default to None.

Relevant issues

Fixes #1791

Type

🐛 Bug Fix / ✅ Test

Changes

  • libs/genai/tests/unit_tests/test_chat_models.py: add _strip_none helper and apply it to both operands of the grounding_metadata equality assertion in test_response_to_result_grounding_metadata.

Testing

Ran the failing tests against both ends of the supported google-genai range:

  • google-genai==1.74.0 — 3 passed.
  • google-genai==1.65.0 (current minimum) — 3 passed.
  • Full module: pytest tests/unit_tests/test_chat_models.py — 219 passed.
  • ruff check, ruff format --check, and mypy clean on the touched file.

…dditions

`test_response_to_result_grounding_metadata` compared the emitted
`grounding_metadata` dict against a frozen expected dict, so any new
optional field added to upstream `google.genai.types` schemas (which
default to `None` when unset) flipped the equality check and broke
the test. `GroundingSupport.rendered_parts` was introduced in
google-genai 1.69.0 and triggered this on supported releases >=1.69.

Strip `None` values from both sides recursively before comparing.
Preserves coverage of every field the test actually populates and
keeps the assertion green across the supported `google-genai>=1.65.0`
range without weakening guarantees.

Fixes langchain-ai#1791
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.

Test failure in test_chat_models.py when google-genai > 1.74.0

1 participant