Skip to content

Commit a9c4e6e

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: allow unknown union variants when validating gaos responses in evals
PiperOrigin-RevId: 986089471
1 parent 5af2f8e commit a9c4e6e

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎agentplatform/_genai/_evals_common.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
from google.genai._gaos.types.interactions import functionresultstep
3838
from google.genai._gaos.types.interactions import modeloutputstep
3939
from google.genai._gaos.types.interactions import userinputstep
40+
41+
try:
42+
from google.genai._gaos.utils.serializers import ALLOW_UNKNOWN_UNION_VARIANTS
43+
except (ImportError, AttributeError):
44+
ALLOW_UNKNOWN_UNION_VARIANTS = "speakeasy_allow_unknown_union_variants"
4045
from google.genai.models import Models
4146
import pandas as pd
4247
from tqdm import tqdm
@@ -775,7 +780,16 @@ def _interaction_dict_to_agent_data(
775780
Returns:
776781
An AgentData object with one or more ConversationTurns.
777782
"""
778-
typed_interaction = interaction_types.Interaction.model_validate(interaction)
783+
# A server response, so unknown step types must degrade rather than raise.
784+
# `Step` is an open discriminated union: parse_open_union only falls back to
785+
# the Unknown variant when the validation context carries
786+
# ALLOW_UNKNOWN_UNION_VARIANTS, and raises without it so that a
787+
# user-constructed request payload surfaces its mistakes locally. This
788+
# payload comes off the Interactions API, so it is on the tolerant side of
789+
# that line. _interaction_steps_to_events already drops steps it cannot map.
790+
typed_interaction = interaction_types.Interaction.model_validate(
791+
interaction, context={ALLOW_UNKNOWN_UNION_VARIANTS: True}
792+
)
779793
all_events = _interaction_steps_to_events(typed_interaction.steps or [])
780794

781795
# Group events into turns. Each UserInputStep starts a new turn.
@@ -1689,8 +1703,12 @@ def _resolve_interactions_to_eval_cases(
16891703
break
16901704
interaction_dict = json.loads(response.body)
16911705
try:
1706+
# Also a server response -- see _interaction_dict_to_agent_data.
1707+
# Without the context a single step type this client does not
1708+
# know about raised, and the except below turned that into a
1709+
# `break`, silently truncating the interaction history.
16921710
typed_interaction = interaction_types.Interaction.model_validate(
1693-
interaction_dict
1711+
interaction_dict, context={ALLOW_UNKNOWN_UNION_VARIANTS: True}
16941712
)
16951713
except Exception as e:
16961714
logger.warning("Failed to validate interaction model: %s", e)

0 commit comments

Comments
 (0)