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

OpenAPI parser doesn't let an operation parameter override the path-level one: the model is asked for the same path value twice, and the wrong one is sent

Open 1aifanatic opened this issue 2 months ago • 4 comments

🔴 Required Information

Describe the Bug:

OpenApiSpecParser._collect_operations() appends path-level parameters to every operation's own parameters without checking for overrides:

operation_dict["parameters"] = operation_dict.get("parameters", []) + path_item.get("parameters", [])

The OpenAPI 3 Path Item Object says an operation-level parameter with the same name and in overrides the path-level one. Declaring the shared path parameter once at the path level and refining it on an operation (a more specific description, pattern or enum) is common. ADK keeps both, OperationParser de-duplicates the names, and the tool then asks the model for the same URL segment twice as two required arguments: account_id and account_id_0. When the model fills both, both map to the same {accountId} placeholder and the path-level value wins, so the operation's own, more specific parameter is silently dropped from the request.

Steps to Reproduce:

  1. pip install google-adk (reproduced on main @ 3f4bb8fa).
  2. Run the script below: an OpenAPIToolset whose /accounts/{accountId} path declares accountId and whose GET overrides it. HTTP goes to an httpx.MockTransport.

Expected Behavior: One required argument, account_id, described by the operation-level parameter; {"account_id": "ACC-123"} requests /accounts/ACC-123.

Observed Behavior:

model is asked for: ['account_id', 'account_id_0']
args: {'account_id': 'ACC-123', 'account_id_0': 'ACC-999'} -> request: https://crm.example.com/accounts/ACC-999

Environment Details:

  • ADK Library Version (pip show google-adk): main @ 3f4bb8fa
  • Desktop OS: Windows 11
  • Python Version (python -V): 3.12.10

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: N/A (tool declaration and request building)

🟡 Optional Information

Minimal Reproduction Code:

import asyncio

import httpx
from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_toolset import OpenAPIToolset

spec = {
    "openapi": "3.0.0",
    "info": {"title": "CRM", "version": "1"},
    "servers": [{"url": "https://crm.example.com"}],
    "paths": {"/accounts/{accountId}": {
        "parameters": [{"name": "accountId", "in": "path", "required": True,
                        "schema": {"type": "string"}, "description": "Shared account id"}],
        "get": {
            "operationId": "getAccount",
            "parameters": [{"name": "accountId", "in": "path", "required": True,
                            "schema": {"type": "string", "pattern": "^ACC-[0-9]+$"},
                            "description": "Account id, e.g. ACC-123"}],
            "responses": {"200": {"description": "ok"}},
        },
    }},
}
sent = []
toolset = OpenAPIToolset(
    spec_dict=spec,
    httpx_client_factory=lambda: httpx.AsyncClient(transport=httpx.MockTransport(
        lambda r: sent.append(str(r.url)) or httpx.Response(200, json={}))),
)


async def main():
  tool = (await toolset.get_tools())[0]
  schema = tool._get_declaration().parameters_json_schema
  print("model is asked for:", schema["required"])
  args = {"account_id": "ACC-123"}
  if "account_id_0" in schema["properties"]:
    args["account_id_0"] = "ACC-999"  # a model filling both required fields
  await tool.run_async(args=args, tool_context=None)
  print("args:", args, "-> request:", sent[0])


asyncio.run(main())

Suggested fix: when merging, skip path-level parameters whose (name, in) the operation already declares. Parameters with the same name but a different location are still merged. I have a PR ready with a test.

How often has this issue occurred?:

  • Always (100%): any spec that overrides a path-level parameter on an operation.

1aifanatic avatar Sep 19 '26 05:09 1aifanatic

Opened #7219 for this: _collect_operations() now merges the parameters with a _merge_parameters() helper that keys each parameter on (name, in) and drops the path-level entries the operation re-declares, so the duplicate account_id_0 no longer reaches the declaration. Parameters missing name/in are untouched. Flagging it here in case anyone else has started on it.

CodeTrainerMan avatar Sep 20 '26 20:09 CodeTrainerMan

Hello @1aifanatic and @CodeTrainerMan,

Thank you for your contributions.

We were able to reproduce the issue and tested both linked PRs. Both seem to resolve the issue by overriding path level parameters with operation level parameters.

However could you please share your thoughts on a specific edge case? What would happen if required=True is defined at the path level but accidentally omitted at the operation level? After the override would it default to False? The model might then treat it as an optional parameter which could cause errors.

llalitkumarrr avatar Sep 22 '26 09:09 llalitkumarrr

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 Sep 30 '26 06:09 adk-bot

Thanks for raising the required edge case. I answered it and updated #7209 on September 22, but missed copying the answer here: https://github.com/google/adk-python/pull/7209#issuecomment-5777771414

The current PR treats every in: path parameter as required, including when an operation-level override omits required, so accountId remains required and is exposed only once. The regression test test_parse_spec_path_parameter_is_required_even_when_override_omits_it covers that case and checks that a query parameter without the flag remains optional. For non-path parameters, the operation-level definition replaces the path-level definition, including its required flag.

The fix and test are already in the current PR. Please keep this issue open pending review and clear the stale/request-clarification labels if this addresses the question.

1aifanatic avatar Sep 30 '26 12:09 1aifanatic