Skip to content

fix(agents): support dict and types.Schema output_schema in task mode - #7966

Closed
shwetangsinha0509 wants to merge 1 commit into
google:mainfrom
shwetangsinha0509:fix/finish-task-dict-output-schema
Closed

shwetangsinha0509 wants to merge 1 commit into
google:mainfrom
shwetangsinha0509:fix/finish-task-dict-output-schema

Conversation

@shwetangsinha0509

Copy link
Copy Markdown
Contributor

Fixes #7935

Problem

LlmAgent(mode="task") fails at construction when output_schema is a raw dict or a types.Schema, although SchemaType documents both as supported and they work in the default and single_turn modes. FinishTaskTool passed the schema to TypeAdapter, which reads a dict as a pydantic core schema (SchemaError: Unknown schema type: "object", or KeyError: 'type' for {}) and cannot generate a schema from a types.Schema instance (PydanticSchemaGenerationError). The same failure was fixed for SetModelResponseTool in #5469.

Fix

  • Dict and types.Schema output schemas are converted directly to JSON Schema (model_dump(by_alias=True, exclude_none=True) + lowercase_schema_types, as anthropic_llm.py does) instead of going through TypeAdapter.
  • Their finish_task arguments are accepted without validation, matching how validate_node_data already treats these schema types.
  • The stored schema is deep-copied in _get_declaration, which pops $defs, so the caller's schema is never mutated.
  • Pydantic models, generic aliases and the default schema are unchanged.

Testing plan

Unit tests

6 new tests in tests/unittests/agents/llm/task/test_finish_task_tool.py:

  • dict object schema used directly as the declaration
  • non-object dict schema wrapped under result
  • empty dict schema
  • caller's schema not mutated across _get_declaration calls ($defs hoisting)
  • types.Schema converted to lowercase JSON Schema
  • run_async with a dict schema returns success
$ pytest tests/unittests/agents/llm/task -q
45 passed in 1.97s

$ pytest tests/unittests/agents tests/unittests/workflow tests/unittests/a2a -q -n auto
2 failed, 3273 passed, 57 skipped, 2 xfailed in 119.01s

The 2 failures are test_tool_node.py::test_execute_bash_tool_node_runs_command_after_approval[False|True] (KeyError: 'returncode'). They fail the same way on main without this change: I ran the suite on Windows, and ExecuteBashTool needs bash. Neither test touches finish_task.

Manual check

Builds an LlmAgent with each schema type in the default mode and in task mode:

from google.adk.agents import LlmAgent
from google.genai import types

cases = {
    "dict object": {"type": "object", "properties": {"answer": {"type": "string"}}},
    "empty dict": {},
    "types.Schema": types.Schema(type="OBJECT", properties={"answer": types.Schema(type="STRING")}),
}
for name, schema in cases.items():
    for mode in (None, "task"):
        kw = {} if mode is None else {"mode": mode}
        try:
            LlmAgent(name="a", model="gemini-2.5-flash", output_schema=schema, **kw)
            print(f"{name:13} mode={mode}: ok")
        except Exception as e:
            print(f"{name:13} mode={mode}: {type(e).__name__}: {str(e).splitlines()[0]}")

Before (main):

dict object   mode=None: ok
dict object   mode=task: SchemaError: Unknown schema type: "object"
empty dict    mode=None: ok
empty dict    mode=task: KeyError: 'type'
types.Schema  mode=None: ok
types.Schema  mode=task: PydanticSchemaGenerationError: Unable to generate pydantic-core schema for Schema(

After (this PR):

dict object   mode=None: ok
dict object   mode=task: ok
empty dict    mode=None: ok
empty dict    mode=task: ok
types.Schema  mode=None: ok
types.Schema  mode=task: ok

@shwetangsinha0509

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #7937, which was opened first for the same fix. I missed it before opening this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LlmAgent(mode="task") with a raw dict output_schema fails at construction with SchemaError: Unknown schema type: "object"

2 participants