From a9c4e6ee29111a57ae5fdb637da923b928280a2d Mon Sep 17 00:00:00 2001 From: Yvonne Yu Date: Tue, 22 Sep 2026 10:37:19 -0700 Subject: [PATCH] fix: allow unknown union variants when validating gaos responses in evals PiperOrigin-RevId: 986089471 --- agentplatform/_genai/_evals_common.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/agentplatform/_genai/_evals_common.py b/agentplatform/_genai/_evals_common.py index 7cb197d8a6..db85f3819d 100644 --- a/agentplatform/_genai/_evals_common.py +++ b/agentplatform/_genai/_evals_common.py @@ -37,6 +37,11 @@ from google.genai._gaos.types.interactions import functionresultstep from google.genai._gaos.types.interactions import modeloutputstep from google.genai._gaos.types.interactions import userinputstep + +try: + from google.genai._gaos.utils.serializers import ALLOW_UNKNOWN_UNION_VARIANTS +except (ImportError, AttributeError): + ALLOW_UNKNOWN_UNION_VARIANTS = "speakeasy_allow_unknown_union_variants" from google.genai.models import Models import pandas as pd from tqdm import tqdm @@ -775,7 +780,16 @@ def _interaction_dict_to_agent_data( Returns: An AgentData object with one or more ConversationTurns. """ - typed_interaction = interaction_types.Interaction.model_validate(interaction) + # A server response, so unknown step types must degrade rather than raise. + # `Step` is an open discriminated union: parse_open_union only falls back to + # the Unknown variant when the validation context carries + # ALLOW_UNKNOWN_UNION_VARIANTS, and raises without it so that a + # user-constructed request payload surfaces its mistakes locally. This + # payload comes off the Interactions API, so it is on the tolerant side of + # that line. _interaction_steps_to_events already drops steps it cannot map. + typed_interaction = interaction_types.Interaction.model_validate( + interaction, context={ALLOW_UNKNOWN_UNION_VARIANTS: True} + ) all_events = _interaction_steps_to_events(typed_interaction.steps or []) # Group events into turns. Each UserInputStep starts a new turn. @@ -1689,8 +1703,12 @@ def _resolve_interactions_to_eval_cases( break interaction_dict = json.loads(response.body) try: + # Also a server response -- see _interaction_dict_to_agent_data. + # Without the context a single step type this client does not + # know about raised, and the except below turned that into a + # `break`, silently truncating the interaction history. typed_interaction = interaction_types.Interaction.model_validate( - interaction_dict + interaction_dict, context={ALLOW_UNKNOWN_UNION_VARIANTS: True} ) except Exception as e: logger.warning("Failed to validate interaction model: %s", e)