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

Add `response_json_schema` support to `RunConfig` for per-request structured output

Open lithammer opened this issue 11 months ago • 6 comments

Is your feature request related to a problem? Please describe.

When building A2A (Agent-to-Agent) servers using ADK, the calling agent can request structured output by including a JSON schema in the message metadata, per the A2A Structured Data Exchange spec:

{
  "message": {
    "parts": [{
      "text": "Show me a list of my open IT tickets",
      "metadata": {
        "mediaType": "application/json",
        "schema": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "ticketNumber": { "type": "string" },
              "description": { "type": "string" }
            }
          }
        }
      }
    }]
  }
}

The schema is per-request, not per-agent. Different requests to the same agent may request different output schemas (or no schema at all).

Currently, there's no direct way to pass a response schema through Runner.run_async(). The available options are:

  1. LlmAgent.output_schema - Set at agent creation time, not per-request
  2. LlmAgent.generate_content_config - Also set at agent creation time
  3. before_model_callback / Plugin - Works, but requires storing the schema in session state and reading it back in a callback, which is indirect

Describe the solution you'd like

Add optional fields to RunConfig:

class RunConfig(BaseModel):
    # ... 

    response_json_schema: Optional[dict[str, Any]] = None
    """JSON Schema for structured output. When set, configures the LLM
    to return responses conforming to this schema."""

    response_mime_type: Optional[str] = None
    """MIME type for the response. Defaults to 'application/json' when
response_json_schema is set."""

Then in BaseLlmFlow, apply these to the LlmRequest.config before calling the LLM:

if invocation_context.run_config and invocation_context.run_config.response_json_schema:
    llm_request.config = llm_request.config or types.GenerateContentConfig()
    llm_request.config.response_json_schema = invocation_context.run_config.response_json_schema
    llm_request.config.response_mime_type = (
        invocation_context.run_config.response_mime_type or "application/json"
    )

Example Usage

schema = {
    "type": "object",
    "properties": {
        "answer": {"type": "string"},
        "confidence": {"type": "number"}
    }
}

async for event in runner.run_async(
    user_id="user-123",
    session_id="session-456",
    new_message=content,
    run_config=RunConfig(response_json_schema=schema),
):
    # LLM responses will conform to the schema
    ...

Describe alternatives you've considered

Using a plugin/callback. This works but requires:

  1. Storing the schema in session state before calling run_async()
  2. Creating a plugin that reads from session state in before_model_callback
  3. Setting it on llm_request.config.response_json_schema

This is indirect and couples the schema to session state rather than the request itself.

Additional context

This started with me trying to send A2A requests to kagent (which uses ADK under the hood) including as the documentation says. But the schema information is dropped by ADK.

lithammer avatar Dec 08 '25 12:12 lithammer

Notification: The author has updated the issue description. Maintainers, please review.

adk-bot avatar Dec 18 '25 23:12 adk-bot

This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

adk-bot avatar Dec 27 '25 06:12 adk-bot

It's not stale and no maintainer has requested anything as far as I know.

lithammer avatar Dec 27 '25 13:12 lithammer

This looks like a good FR to me. Asking it to be added to the backlog.

zeroasterisk avatar Feb 09 '26 06:02 zeroasterisk

@lithammer Thanks for the update. The request is currently being reviewed, and I will let you know if anything further is needed. Thank you for understanding.

klateefa avatar Mar 12 '26 08:03 klateefa

On current main (7d3658e), RunConfig(response_json_schema=...) is still rejected as an extra field. I can work on passing a request-local schema to the model without mutating the shared agent.

Would you want this to apply only to the root agent's final response, or to every LLM call in the invocation? Also, should the agent's existing output_schema take precedence, as the RunConfig docstring says for agent-specific settings? I would keep A2A metadata mapping out of the first change.

Samecho avatar Oct 14 '26 23:10 Samecho