Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/google/adk/workflow/utils/_rehydration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,22 @@ def _validate_resume_response(response_data: object, schema: object) -> object:
required = schema.get('required', [])

fields: dict[str, Any] = {}
no_default: set[str] = set()
for prop_name, prop_schema in properties.items():
prop_type_str = prop_schema.get('type')
prop_type = (
type_mapping.get(prop_type_str, Any) if prop_type_str else Any
)

if 'default' not in prop_schema:
no_default.add(prop_name)

if prop_name in required:
fields[prop_name] = (prop_type, ...)
else:
fields[prop_name] = (
prop_type | None,
None,
prop_schema.get("default"),
)

try:
Expand All @@ -298,7 +302,14 @@ def _validate_resume_response(response_data: object, schema: object) -> object:
model_instance = TypeAdapter(DynamicModel).validate_python(
response_data
)
return model_instance.model_dump()
# A property without a JSON-schema default (e.g. a Pydantic
# default_factory) gets its value from the downstream model, so an
# omitted answer must not emit it as None. Pydantic tracks the fields
# the answer actually carried, including an explicit None, so only
# the untouched no-default fields are left out.
return model_instance.model_dump(
exclude=no_default - model_instance.model_fields_set
)
except ValidationError as e:
raise WorkflowDataError(
f'Validation failed for object schema: {e}'
Expand Down
96 changes: 96 additions & 0 deletions tests/unittests/workflow/utils/test_rehydration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from google.adk.workflow.utils._workflow_hitl_utils import create_request_input_event
from google.genai import types
from pydantic import BaseModel
from pydantic import Field
import pytest

# --- _wrap_response ---
Expand Down Expand Up @@ -257,6 +258,101 @@ def test_string_schema_is_not_evaluated(self, tmp_path):
assert _validate_resume_response("data", schema) == "data"
assert not marker.exists()

def test_object_schema_uses_declared_defaults_for_missing_fields(self):
"""A partial resume answer keeps the optional fields' schema defaults."""

class Decision(BaseModel):
approved: bool
priority: int = 3
tags: list[str] = []
reviewer: str = "manager"

answer = _validate_resume_response(
{"approved": True}, Decision.model_json_schema()
)

assert answer == {
"approved": True,
"priority": 3,
"tags": [],
"reviewer": "manager",
}
# The next node rebuilds the model from this answer; it must not fail.
assert Decision.model_validate(answer) == Decision(approved=True)

def test_object_schema_explicit_values_override_declared_defaults(self):
"""A provided optional field wins over its schema default."""

class Decision(BaseModel):
approved: bool
priority: int = 3
tags: list[str] = []
reviewer: str = "manager"

answer = _validate_resume_response(
{
"approved": False,
"priority": 7,
"tags": ["urgent"],
"reviewer": "director",
},
Decision.model_json_schema(),
)

assert answer == {
"approved": False,
"priority": 7,
"tags": ["urgent"],
"reviewer": "director",
}

def test_object_schema_leaves_out_omitted_fields_without_default(self):
"""A default_factory field carries no JSON-schema default to restore."""

class Decision(BaseModel):
approved: bool
tags: list[str] = Field(default_factory=list)

answer = _validate_resume_response(
{"approved": True}, Decision.model_json_schema()
)

assert answer == {"approved": True}
# The downstream node rebuilds the model: the factory fills tags in.
assert Decision.model_validate(answer) == Decision(approved=True)

def test_object_schema_keeps_given_fields_without_default(self):
"""An answer's own value for a no-default field is kept, including None."""

class Decision(BaseModel):
approved: bool
tags: list[str] | None = Field(default_factory=list)

answer = _validate_resume_response(
{"approved": True, "tags": None}, Decision.model_json_schema()
)
assert answer == {"approved": True, "tags": None}

answer = _validate_resume_response(
{"approved": True, "tags": ["urgent"]}, Decision.model_json_schema()
)
assert answer == {"approved": True, "tags": ["urgent"]}

def test_resume_answer_omitting_optional_fields_keeps_defaults(self):
"""No-default fields are dropped while declared defaults still apply."""

class Decision(BaseModel):
approved: bool
priority: int = 3
tags: list[str] = Field(default_factory=list)

answer = _validate_resume_response(
{"approved": True}, Decision.model_json_schema()
)

assert answer == {"approved": True, "priority": 3}
assert Decision.model_validate(answer) == Decision(approved=True)


# --- _reconstruct_node_states ---

Expand Down