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

ApigeeLlm Chat Completions crashes on typed tool result values

Open 1aifanatic opened this issue 3 weeks ago • 2 comments

Describe the bug

ApigeeLlm's Chat Completions adapter calls json.dumps directly on tool responses. A normal Python tool returning a date, datetime, Decimal, UUID, or Pydantic model therefore crashes before sending the next HTTP request. This affects both streaming and non-streaming requests.

Reproduction

Use an ApigeeLlm configured for an OpenAI-compatible Chat Completions endpoint with this tool:

import datetime
from decimal import Decimal

def get_invoice() -> dict:
    return {'due': datetime.date(2026, 12, 1), 'total': Decimal('12.50')}

When the model calls get_invoice, the result becomes a FunctionResponse successfully, but conversion to the next Chat Completions request raises TypeError: Object of type date is not JSON serializable in CompletionsHTTPClient._contents_to_messages.

Expected: send valid JSON with the date encoded as an ISO string and Decimal encoded without losing precision, consistent with ADK's typed model values. Ordinary JSON results should remain unchanged.

Environment and validation

Reproduced on main at 12aab4aaa, Python 3.12.10, Windows. A real InMemoryRunner and FunctionTool were exercised against a local HTTP server emitting OpenAI-compatible responses, for both NONE and SSE streaming. No live Apigee deployment or provider credentials were used.

This is separate from #8123, which affects the LiteLlm adapter. Apigee has its own serializer and raises rather than falling back to a Python repr. The proposed fix uses the already-installed Pydantic serializer only for values unsupported by the standard JSON encoder.

Prepared with AI assistance; reproduction and validation were executed locally.

1aifanatic avatar Nov 13 '26 21:11 1aifanatic

@1aifanatic. Reproduced this on 2.14.1 and current main (also on 1.27.2) with a local fake endpoint: the get_invoice result fails at apigee_llm.py:850 before the second request goes out.

As a workaround I passed default=lambda o: to_jsonable_python(o, by_alias=False, bytes_mode='base64') to that json.dumps call. The tool message then came through as {"due": "2026-12-01", "total": "12.50"}, and plain dict results were unchanged.

Could you check that this matches what you see on your side, including SSE? It’s also worth trying a nested Pydantic model and bytes, since those take the base64 path.

Once it holds up in your testing, please take #8134 out of draft so it can go to review. It would be good to keep the options in line with the Gemma fix and #8125.

surajksharma07 avatar Nov 18 '26 19:11 surajksharma07

Hey @1aifanatic thanks for contributing to this! An internal team will be taking over from here and your work will be credited.

willie3838 avatar Dec 03 '26 16:12 willie3838