ApigeeLlm Chat Completions path sends max_tokens to gpt-5 and o-series models
Found while testing LLM provider integrations in agent frameworks: an automated tool flags candidates and each one is reproduced before it is reported. Reproduced on 58332a2.
Describe the Bug:
ApigeeLlm on the Chat Completions path (apigee/openai/<model>) always sends GenerateContentConfig.max_output_tokens as max_tokens. OpenAI's reasoning models (o-series, gpt-5 family) don't accept max_tokens on Chat Completions and take max_completion_tokens instead, so an agent that sets a token limit can't use these models through Apigee. OpenAILlm already switches the key for models matched by _openai_common.is_reasoning_model.
Steps to Reproduce:
pip install google-adk(or a checkout at 58332a2)- Run the snippet below. It swaps the HTTP transport for an
httpx.MockTransportand prints the token limit that would be sent.
Expected Behavior:
For gpt-5-mini (and o3, o4-mini and similar) the limit is sent as "max_completion_tokens": 1024.
Observed Behavior:
gpt-5-mini {'max_tokens': 1024}
I did not send this to a live endpoint. The Chat Completions reference says max_tokens "is now deprecated in favor of max_completion_tokens, and is not compatible with o-series models", and OpenAI rejects it with a 400 for these models.
Environment Details:
- ADK Library Version (pip show google-adk): 2.13.0 (main at 58332a2)
- Desktop OS: Linux
- Python Version (python -V): 3.12.4
Model Information:
- Are you using LiteLLM: No
- Which model is being used:
ApigeeLlm(model="apigee/openai/gpt-5-mini")
Additional Context:
CompletionsHTTPClient._map_config_parameters writes the key without looking at the model: https://github.com/google/adk-python/blob/58332a284b73435eb5c54dac73f6740258b7a741/src/google/adk/models/apigee_llm.py#L741-L742
A fix is to send max_completion_tokens when is_reasoning_model(payload['model']) is true (the helper in integrations/openai/_openai_common.py has no openai dependency) and keep max_tokens for other models, so OpenAI-compatible backends that only know max_tokens keep working.
Minimal Reproduction Code:
import asyncio, json, httpx
from google.adk.models import apigee_llm
from google.adk.models.llm_request import LlmRequest
from google.genai import types
def handler(request):
body = json.loads(request.content)
print(body['model'], {k: body[k] for k in ('max_tokens', 'max_completion_tokens') if k in body})
return httpx.Response(200, json={'choices': [{'message': {'role': 'assistant', 'content': 'Hello!'}}]})
real_client = httpx.AsyncClient
apigee_llm.httpx.AsyncClient = lambda *a, **kw: real_client(*a, **{**kw, 'transport': httpx.MockTransport(handler)})
llm = apigee_llm.ApigeeLlm(model='apigee/openai/gpt-5-mini', proxy_url='https://proxy.example.com')
request = LlmRequest(model=llm.model, config=types.GenerateContentConfig(max_output_tokens=1024),
contents=[types.Content(role='user', parts=[types.Part(text='Hi')])])
async def main():
async for _ in llm.generate_content_async(request):
pass
asyncio.run(main())
How often has this issue occurred?:
- Always (100%)
I reproduced this on current main. Is anyone already working on the token-limit mapping? I can reuse is_reasoning_model and add request-level regression tests, keeping max_tokens for the other compatible backends.
Not on my side, go ahead. Thanks for picking it up!
@swf1996120 Reproduced this on 2.13.0 too, and gpt-5.x / o3 / o4-mini all still go out with max_tokens.
As a workaround, could you try wrapping CompletionsHTTPClient._map_config_parameters so it renames max_tokens to max_completion_tokens when is_reasoning_model(payload["model"]) is true? gpt-4o and the -chat models stay as they are. It fixed the payload in my local test. Please give it a go against your actual Apigee endpoint and let me know if the request goes through.
@Samecho since #7866 takes the same approach, could you also test it end to end with a real reasoning model and confirm it works before we move ahead with the review?
Thanks for looking into this. I don't have an Apigee endpoint to test against, so I can't confirm a live request, sorry.
What I could check: with your wrapper applied, the repro from the issue (mocked transport, adk 2.13.0) sends max_completion_tokens: 1024 for gpt-5-mini and o4-mini, and gpt-4o still gets max_tokens. Without the wrapper it still sends max_tokens for all three.
Thanks @swf1996120, that’s enough for this one. The payload is what matters here, and OpenAI documents that reasoning models reject max_tokens, so a live Apigee call isn’t needed to confirm it.
Went through #7866 and it’s the same change as the workaround. It checks the model ID after _get_model_id strips the apigee/openai/ prefix, and it imports is_reasoning_model from _openai_common, which doesn’t pull in the openai SDK. It also still merges cleanly on current main after the recent Apigee structured-output fix. Nothing up to 2.13.0 or on main has this fix yet.
@Samecho a live run against a real o-series or gpt-5 model would still be nice if you have access, but the mocked request tests cover the change, so I’ll flag #7866 for maintainer review.
Separately, these models also reject non-default temperature/top_p, which ApigeeLlm still forwards as-is. That’s better as its own issue than part of this PR.