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

未關閉 適合新手
#8,359 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 3 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
82/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
ai

研究方向

此錯誤位於 src/google/adk/workflow/utils/_rehydration_utils.py 中的 _validate_resume_response,它會重建 response_schema 模型,並為非必填欄位設定 None 預設值。請先執行 issue 中的重現步驟,查看三個 ValidationError,接著閱讀該函式如何從 JSON schema 建立模型。當部分回答能解析為宣告的預設值,且 tests/unittests/workflow/utils/test_rehydration_utils.py 中的迴歸測試與現有的 rehydration 和 HITL 測試一起通過時,即為完成。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Python
星號
22.3k
分支
4.4k
平均合併
1 天 2 小時
30 天內合併 PR
6

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

google/adk-python 的其他 Issue

查看 google/adk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。