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

Structured Output (output schema) + Tool Call

Open borso271 opened this issue 1 year ago • 17 comments

Current behavior: According to the documentation (Agents → LLM Agents → Structuring Data):

“Constraint: Using output_schema enables controlled generation within the LLM but disables the agent's ability to use tools or transfer control to other agents. Your instructions must guide the LLM to produce JSON matching the schema directly.”

Why this is limiting: The inability to combine structured output with tool usage significantly reduces the expressiveness and modularity of agentic workflows. Many real-world applications require both:

Other agent frameworks (e.g., OpenAI's Agents SDK, LangChain) already support coexisting tool calls and structured output by differentiating between “LLM wants to act” vs “LLM wants to reply.”

We would love to know:

  • Is this feature on the roadmap?

  • Are there internal blockers (model behavior / system design) that prevent this from being supported soon?

Thank you! This improvement would greatly expand the viability of the framework in production use.

borso271 avatar May 13 '25 11:05 borso271

@borso271 thanks for reaching out! We're sorting this out with the Gemini backend teams.

Related to https://github.com/google/adk-python/discussions/84

Jacksunwei avatar May 13 '25 23:05 Jacksunwei

Greetings, to solve the issue of combining tool calling with structured output schema, I combined a SequentialAgent that integrates two LLM Agents: the first generates unstructured responses while supporting tool calls, and the second transforms this output into the schema-defined format. SequentialAgent first invokes the tool-enabled agent and then passes its results to the schema formatter. It seems to work for now but supporting tool calling with output_schema should be a future feature. Below there is a sample code of an agentic rag that uses a pydantic schema called 'AISystemDocumentation ' and also calls a tool called 'query_rag_tool'

query_agent = LlmAgent(
    name="call_rag_agent",
    model=LiteLlm(model="openai/gpt-4o"),
    description=(
        "This is  an agent that asks questions to a RAG system."
    ),
    instruction=(
        "You are an agent that asks questions to a RAG system"
    ),
    tools=[query_rag_tool],
    output_key="answers",
)

formater_agent = LlmAgent(
    name="format_agent",
    model=LiteLlm(model="openai/gpt-4o"),
    description=(
        """
        This is an agent that formats the answers from the agent 'query_agent'.
        """
    ),
    instruction=(
        """
        You are an agent that formats the answers from the RAG system.
        """
    ),
    output_schema=AISystemDocumentation
)

root_agent = SequentialAgent(
    name="root_agent",
    description=(
        """
        This is the root agent that coordinates the ingestion and querying process to the RAG system.
        """
    ),
    sub_agents=[query_agent, formater_agent],
)

mcharatzoglou avatar May 16 '25 15:05 mcharatzoglou

Apart from the fact that output_schema is not currently supported with sub_agents/tools, it also confuses me that the output_schema doesn't accept a list.

Imagine a case where one of the tools can respond with a user object which will be 1 schema, another can have a list of users(or another enitty) which will be another schema. Both of these tools should be OK to be called from one agent, so how will a single output schema work?

I think the output_schema should either be a key/value pair or it should(also?) be a property of the tool.

saranshdhingra avatar Jun 24 '25 11:06 saranshdhingra

The SequentialAgent workaround was a bit clunky for my use case, so I implemented a different solution.

You can add a before_model_callback to your agent.

This code is for a different frontend, but the idea can be applied to your use case. Basically, it can check for an output schema in the prompt and then enforce it + remove all tools if it is found.

def set_structured_output(
    callback_context: CallbackContext, llm_request: LlmRequest
) -> None:
    """
    Set the JSON schema for structured output.
    If a JSON Schema is set, all tools are removed as google-adk does not support tools + structured output right now.
    """
    if not llm_request.contents or not llm_request.contents[-1].parts:
        return

    SCHEMA_START = '```jsonSchema\n'
    SCHEMA_END = '\n```'

    last_parts = llm_request.contents[-1].parts

    for i in range(len(last_parts) - 1, -1, -1):
        part = last_parts[i]
        if (
            part.text
            and part.text.startswith(SCHEMA_START)
            and part.text.endswith(SCHEMA_END)
        ):
            schema_text = part.text.removeprefix(SCHEMA_START).removesuffix(
                SCHEMA_END
            )

            del last_parts[i]
            llm_request.tools_dict.clear()
            llm_request.config.tools = None
            llm_request.config.response_json_schema = json.loads(schema_text)
            llm_request.config.response_mime_type = 'application/json'
            return
root_agent = Agent(
    model=MODEL,
    name=NAME,
    description=DESCRIPTION,
    instruction=SYSTEM_PROMPT,
    tools=[google_search],
    before_model_callback=set_structured_output,
)

Sharing this as it may be helpful to someone 😄

cschmidterNBTC avatar Jul 01 '25 14:07 cschmidterNBTC

Greetings, to solve the issue of combining tool calling with structured output schema, I combined a SequentialAgent that integrates two LLM Agents: the first generates unstructured responses while supporting tool calls, and the second transforms this output into the schema-defined format. SequentialAgent first invokes the tool-enabled agent and then passes its results to the schema formatter. It seems to work for now but supporting tool calling with output_schema should be a future feature. Below there is a sample code of an agentic rag that uses a pydantic schema called 'AISystemDocumentation ' and also calls a tool called 'query_rag_tool'

query_agent = LlmAgent(
    name="call_rag_agent",
    model=LiteLlm(model="openai/gpt-4o"),
    description=(
        "This is  an agent that asks questions to a RAG system."
    ),
    instruction=(
        "You are an agent that asks questions to a RAG system"
    ),
    tools=[query_rag_tool],
    output_key="answers",
)

formater_agent = LlmAgent(
    name="format_agent",
    model=LiteLlm(model="openai/gpt-4o"),
    description=(
        """
        This is an agent that formats the answers from the agent 'query_agent'.
        """
    ),
    instruction=(
        """
        You are an agent that formats the answers from the RAG system.
        """
    ),
    output_schema=AISystemDocumentation
)

root_agent = SequentialAgent(
    name="root_agent",
    description=(
        """
        This is the root agent that coordinates the ingestion and querying process to the RAG system.
        """
    ),
    sub_agents=[query_agent, formater_agent],
)

That's a good point @mcharatzoglou. Using a sequence of agents for tool use and formatting is a clean architectural approach.

I've been thinking about the implementation details, and one thing that comes to mind is token usage. My understanding is that in this setup, the formatter agent would receive the full context of the conversation (because sub_agent behavior), not just the direct output from the tool agent.

While this ensures the formatter has all the information, it could also lead to significant token consumption, especially in longer conversations. I hope they can make LlmAgent capable to use tools and output_schema

claudio-ayma avatar Jul 02 '25 20:07 claudio-ayma

The SequentialAgent workaround was a bit clunky for my use case, so I implemented a different solution.

You can add a before_model_callback to your agent.

This code is for a different frontend, but the idea can be applied to your use case. Basically, it can check for an output schema in the prompt and then enforce it + remove all tools if it is found.

def set_structured_output( callback_context: CallbackContext, llm_request: LlmRequest ) -> None: """ Set the JSON schema for structured output. If a JSON Schema is set, all tools are removed as google-adk does not support tools + structured output right now. """ if not llm_request.contents or not llm_request.contents[-1].parts: return

SCHEMA_START = '```jsonSchema\n'
SCHEMA_END = '\n```'

last_parts = llm_request.contents[-1].parts

for i in range(len(last_parts) - 1, -1, -1):
    part = last_parts[i]
    if (
        part.text
        and part.text.startswith(SCHEMA_START)
        and part.text.endswith(SCHEMA_END)
    ):
        schema_text = part.text.removeprefix(SCHEMA_START).removesuffix(
            SCHEMA_END
        )

        del last_parts[i]
        llm_request.tools_dict.clear()
        llm_request.config.tools = None
        llm_request.config.response_json_schema = json.loads(schema_text)
        llm_request.config.response_mime_type = 'application/json'
        return

root_agent = Agent( model=MODEL, name=NAME, description=DESCRIPTION, instruction=SYSTEM_PROMPT, tools=[google_search], before_model_callback=set_structured_output, )

Sharing this as it may be helpful to someone 😄

You could also do:

 llm_request.set_output_schema(`your schema`) in the before_model_callback callback

gogasca avatar Jul 27 '25 00:07 gogasca

Seems maybe this has landed and this issue can be closed? Not sure.

https://github.com/google/adk-python/discussions/84#discussioncomment-14157066

https://github.com/google/adk-python/commit/af635674b5d3c128cf21737056e091646283aeb7

pitta-bread avatar Aug 29 '25 09:08 pitta-bread

Yes confirmed, it is in the release notes of v1.11.0: https://github.com/google/adk-python/releases/tag/v1.11.0

@hangfei

pitta-bread avatar Aug 29 '25 09:08 pitta-bread

Yes confirmed, it is in the release notes of v1.11.0: https://github.com/google/adk-python/releases/tag/v1.11.0

@hangfei

This still seems to be buggy. Is this only to work with gemini models?

2025-09-02 18:50:40,982 - WARNING - llm_agent.py:491 - Invalid config for agent person_info_agent: output_schema cannot co-exist with agent transfer configurations. Setting disallow_transfer_to_parent=True, disallow_transfer_to_peers=True 2025-09-02 18:50:41,008 - ERROR - adk_web_server.py:1038 - Error in event_generator: 'NoneType' object has no attribute 'required' Traceback (most recent call last): File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/cli/adk_web_server.py", line 1028, in event_generator async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/runners.py", line 248, in run_async async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/runners.py", line 244, in _run_with_trace async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/runners.py", line 288, in _exec_with_plugin async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/runners.py", line 238, in execute async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/agents/base_agent.py", line 248, in run_async async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/agents/base_agent.py", line 238, in _run_with_trace async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/agents/llm_agent.py", line 288, in _run_async_impl async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 401, in run_async async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 437, in _run_one_step_async async for llm_response in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 761, in _call_llm_async async for event in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 745, in _call_llm_with_tracing async for llm_response in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 937, in _run_and_handle_error raise model_error File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 921, in _run_and_handle_error async for response in agen: File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/models/lite_llm.py", line 740, in generate_content_async _get_completion_inputs(llm_request) File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/models/lite_llm.py", line 565, in _get_completion_inputs tools = [ ^ File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/models/lite_llm.py", line 566, in _function_declaration_to_tool_param(tool) File "/Users/5155106/mystuff/ai_stuff/google-agents/.venv/lib/python3.11/site-packages/google/adk/models/lite_llm.py", line 395, in _function_declaration_to_tool_param if function_declaration.parameters.required: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'required'

@hangfei

Shades-en avatar Sep 02 '25 13:09 Shades-en

This is actually buggy to some extent. From my personal experience - schema_output and tools does not work well together if the tool is an MCP tool. I encountered same error while doing so. But, if you try to define the tool in the same repo (without any mcp protocol), it should work. Although you'll get a similar warning (WARNING - llm_agent.py:491 - Invalid config for agent person_info_agent: output_schema cannot co-exist with agent transfer configurations. Setting disallow_transfer_to_parent=True, disallow_transfer_to_peers=True) but it will work.

It would be really good if google team adds the MCP support as well

noob-hu-yaar avatar Sep 02 '25 14:09 noob-hu-yaar

well i found a way around for this, temporarily, until google team fixes this

noob-hu-yaar avatar Sep 03 '25 14:09 noob-hu-yaar

Supposedly, the new releases already solved this and allow using output_SCHEMA with calls to agents and tools, but it works for me in the ADK web, not in my actual frontend. In my frontend, I actually get errors related to the output scheme and transfer.

camilopenalver avatar Sep 14 '25 01:09 camilopenalver

Yeah, the output schema and tools does work together...but what i experienced is that if the tool is in form of a MCP tool call, it will fail. Had to do some modifications to make mcp tool call compatible. For non-mcp tools alongside output schema works fine

noob-hu-yaar avatar Sep 16 '25 10:09 noob-hu-yaar

hi @noob-hu-yaar can u explain here what modification u made to make it work.

malhotradi avatar Sep 25 '25 10:09 malhotradi

It's supported now. Please try our latest version. Closing this.

hangfei avatar Nov 04 '25 20:11 hangfei

Even in version 1.18.0 using output_schema with MCP tools still does not work for me

ArnePannemans avatar Nov 18 '25 11:11 ArnePannemans

I am still experiencing this issue with ADK 1.3.0

kaifresh avatar May 29 '26 04:05 kaifresh