Skip to content

fix(task): support raw dict and types.Schema output_schema in FinishTaskTool (#7935) - #7937

Open
mohitdixit02 wants to merge 1 commit into
google:mainfrom
mohitdixit02:fix/finish-task-tool-dict-schema
Open

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

Conversation

@mohitdixit02

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
When creating an LlmAgent in task mode (mode="task") with a raw JSON schema dictionary (or a google.genai.types.Schema instance) as its output_schema, agent initialization failed at construction with:
pydantic_core._pydantic_core.SchemaError: Unknown schema type: "object" (or PydanticSchemaGenerationError).

In FinishTaskTool.__init__, self._adapter: TypeAdapter[Any] = TypeAdapter(self.output_schema) assumed output_schema was always a Python type or Pydantic
model. Because TypeAdapter does not accept raw JSON schema dictionaries or types.Schema instances, construction failed before the agent could be used.

Solution:

  1. In FinishTaskTool.__init__:
    • Set self._adapter = None when output_schema is a raw dict or types.Schema, bypassing Pydantic's TypeAdapter.
    • Pass self.output_schema directly to get_output_wrapper_key.
  2. In FinishTaskTool._get_declaration():
    • When self._adapter is None, obtain the raw schema via copy.deepcopy(schema_to_json_schema(self.output_schema)) to prevent in-place mutation of the user's schema dictionary.
  3. In FinishTaskTool.run_async():
    • When self._adapter is None, pass raw_value through directly, matching the established precedent in SetModelResponseTool and validate_node_data.
  4. In schema_to_json_schema():
    • Added support for types.Schema using model_dump(mode="json", exclude_none=True) and lowercase_schema_types.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Summary of passed pytest results:

pytest tests/unittests/agents/llm/task/test_finish_task_tool.py tests/unittests/utils/test_schema_utils.py

collected 136 items
tests/unittests/agents/llm/task/test_finish_task_tool.py ..................................................... [ 38%]
tests/unittests/utils/test_schema_utils.py ......................................................             [100%]
====================== 136 passed in 1.69s ======================

Manual End-to-End (E2E) Tests:
Verified against the reproduction case with a raw dictionary schema:

    schema = {"type": "object", "properties": {"answer": {"type": "string"}}}
    agent = LlmAgent(name="b", model="gemini-2.5-flash", output_schema=schema, mode="task")
  • Before: Raised pydantic_core._pydantic_core.SchemaError: Unknown schema type: "object".
  • After: Constructs cleanly and registers finish_task tool with unwrapped parameters declaration.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

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