adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Workflow RequestInput: an answer that leaves out optional fields gets None instead of the schema defaults, and resume crashes

Open chaitanya0305 opened this issue 1 week ago • 3 comments

Summary

When a Workflow node pauses with RequestInput(response_schema=SomeModel) and the answer leaves out optional fields, the resumed answer has None for those fields instead of the defaults the model declares. A field such as priority: int = 3 or tags: list[str] = [] arrives as None, so the next node, typed with the same model, fails validation. A valid partial answer crashes the workflow on resume, and a nullable field with a non-None default silently loses its default.

This is the human-in-the-loop pattern from the request_input_advanced sample. Workflow is exported at the top level, and nothing marks this path as experimental.

Reproduction

Run on current main (4e3aff79):

import asyncio
from google.adk import Workflow
from google.adk.events import RequestInput
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.genai import types
from pydantic import BaseModel

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

def ask(node_input: str):
    return RequestInput(interrupt_id="q", message="decide", response_schema=Decision)

def after(node_input: Decision):
    return f"approved={node_input.approved} priority={node_input.priority} tags={node_input.tags} reviewer={node_input.reviewer}"

async def main():
    ss = InMemorySessionService()
    wf = Workflow(name="wf", edges=[("START", ask, after)])
    async def turn(msg):
        r = Runner(app_name="app", node=wf, session_service=ss, auto_create_session=True)
        return [e async for e in r.run_async(user_id="u", session_id="s1", new_message=msg)]
    await turn(types.Content(role="user", parts=[types.Part(text="hi")]))
    answer = types.FunctionResponse(id="q", name="adk_request_input", response={"approved": True})
    for e in await turn(types.Content(role="user", parts=[types.Part(function_response=answer)])):
        print(e.author, "output=", e.output)

asyncio.run(main())

Actual

pydantic_core._pydantic_core.ValidationError: 3 validation errors for dynamic node 'after'
priority
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
tags
  Input should be a valid list [type=list_type, input_value=None, input_type=NoneType]
reviewer
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

Expected

after output= approved=True priority=3 tags=[] reviewer=manager

Cause

_validate_resume_response in src/google/adk/workflow/utils/_rehydration_utils.py rebuilds an object response_schema with pydantic.create_model. Every field that is not required gets the default None, and the default in the JSON schema is ignored.

Proposed fix

For fields that are not required, use prop_schema.get("default") as the default. Required fields behave as now: a missing one still raises, and a given one is still coerced.

I checked mutable defaults ([], {}): they are not shared between answers, because each call builds a new model and model_dump() returns copies. One case stays open: Field(default_factory=list) puts no default into the JSON schema, so such a field still comes back as None. That would need a different approach and is not part of this change.

There is a regression test in tests/unittests/workflow/utils/test_rehydration_utils.py. It fails on main and passes with the fix; the rehydration and HITL workflow tests pass.

I used an AI coding assistant (Claude) to find and analyse this; I reproduced it and checked the fix myself.

I've tested a fix with a regression test: https://github.com/chaitanya0305/adk-python/compare/main...chaitanya0305:adk-python:fix/resume-response-schema-defaults. If this is accepted, I'll open the PR straight away.

chaitanya0305 avatar Nov 25 '26 17:11 chaitanya0305

@chaitanya0305 Reproduced on main (03725e8) and on the 2.15.1 wheel from PyPI: same 3 validation errors on resume.

Using prop_schema.get("default") fixes the reported case for me. For the default_factory gap, what worked locally was to leave the key out entirely when the schema has no default, so the next node's model fills it in (labels came back as []). Could you try that on your branch and check it holds up for dict-typed nodes too?

#8370 already makes the same one-line change, so it might be easier to review that one or build on it than to open a second PR. Either way, please test it thoroughly before raising anything, and keep an eye on #8361, since it touches the same file.

surajksharma07 avatar Nov 27 '26 05:11 surajksharma07

Thanks @surajksharma07, that works. I tried your approach on my branch (rebased on current main):

  • A field with a default in the schema gets that default (prop_schema.get("default")).
  • A field with no default in the schema (e.g. Field(default_factory=list)) is left out of the answer when it is not given, so the next node's model fills it in. A given value, None included, is kept.

End to end with Decision(approved: bool, priority: int = 3, tags: list[str] = Field(default_factory=list), reviewer: str = "manager", note: str | None = None) and the answer {"approved": true}:

  • Node typed with Decision: receives Decision(approved=True), so tags == [].
  • Node typed with dict: receives {"approved": True, "priority": 3, "reviewer": "manager", "note": None}. tags is absent rather than None, since there is no default to apply.

Both pass in resumable and non-resumable mode. test_rehydration_utils.py, test_workflow_hitl.py, test_node_tool.py, test_workflow_hitl_utils.py and test_tool_node.py all pass (344 tests).

I ran the same tests against #8370: the declared-default cases pass, but the default_factory case still fails (tags comes back as None, so the Decision node raises Input should be a valid list, and the dict node gets "tags": None). As you suggested, I won't open a second PR. I'll leave the patch and tests on #8370 for @feiiiiii5 to pick up. Branch: https://github.com/chaitanya0305/adk-python/compare/main...chaitanya0305:adk-python:fix/resume-schema-defaults-v2

I'll keep an eye on #8361 as well. It touches the same file, but a different function (_unwrap_response).

chaitanya0305 avatar Nov 27 '26 06:11 chaitanya0305

Ran your v2 patch on main and on 2.15.1 @chaitanya0305 and got the same results: the typed node gets tags=[], the dict node gets the declared defaults with tags left out, and a missing required field still fails at resume.

@feiiiiii5 could you pull the patch and those three tests from chaitanya0305's branch into #8370, and switch the description to "Fixes #8359"? That way the default_factory case is covered in one PR.

One small thing, not a blocker: an explicit null for a non-nullable field (tags: None) still gets through the resume check and only fails in the next node. That's existing behaviour, so it's probably better as a separate follow-up.

Once that's updated and tested thoroughly, we can get the workflows approved and the review started.

surajksharma07 avatar Nov 27 '26 06:11 surajksharma07