mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
* feat(interactions): continue requires_continuation long decodes with continuation_token hops A foreground Interactions request stops at Google's per-hop output cap with status requires_continuation and a continuation_token. litellm now re-posts the request with previous_interaction_id and continuation_token until the status moves on, merging the hops into one interaction (last id, every hop's steps, summed usage with initial_input_tokens from hop 1), for non-streaming and streaming calls. continuation_token is accepted as a request param, and requires_continuation is a known status for billing. * refactor(gemini interactions): validate streaming chunks with model_validate * fix(interactions): keep the held completion across stream hops, stop on a repeated continuation_token, follow the spec redirect * fix(interactions): add grounding_tool_count per tool type, stop on any seen continuation_token, keep streaming past a [DONE] frame when a hop is pending * ci(interactions): point the spec canary at aistudio.google.com, where Google moved the Interactions OpenAPI spec * test(interactions): scripted Google ends each long decode hop with a [DONE] frame * refactor(interactions): pass requires_continuation and continuation_token through instead of looping hops in the proxy The caller drives the continuation, the same as against Google directly. /interactions accepts continuation_token in place of input so a resume request needs no input. * test(proxy): assert the accepted /interactions bodies pass the required-param check * fix(proxy): keep the router default fallback for input on /interactions, continuation_token stands in for it --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
356 lines
15 KiB
Python
356 lines
15 KiB
Python
import json
|
|
from pathlib import Path
|
|
from typing import Final
|
|
|
|
import pytest
|
|
from integration._support.client import Gateway
|
|
from integration._support.process import owned_proxy_process
|
|
from integration._support.wire import Reply, Request, wire_server
|
|
from pydantic import BaseModel
|
|
|
|
_GEMINI_MODEL: Final = "gemini/gemini-2.5-flash"
|
|
_GEMINI_DEPLOYMENT_KEY: Final = "synthetic-gemini-deployment-key"
|
|
|
|
_MODEL_BODY: Final = {
|
|
"input": "plan a trip",
|
|
"system_instruction": "be brief",
|
|
"tools": [{"type": "function", "name": "search", "parameters": {"type": "object"}}],
|
|
"generation_config": {"temperature": 0.3, "image_config": {"aspect_ratio": "16:9"}},
|
|
"response_mime_type": "application/json",
|
|
"response_format": {"type": "object", "properties": {"plan": {"type": "string"}}},
|
|
"previous_interaction_id": "interaction-prev-123",
|
|
"store": True,
|
|
"background": True,
|
|
}
|
|
|
|
_INTERACTION_REPLY: Final = {
|
|
"id": "interaction-wire-1",
|
|
"object": "interaction",
|
|
"model": "gemini-2.5-flash",
|
|
"status": "completed",
|
|
"created": "2026-05-20T00:00:00Z",
|
|
"steps": [{"type": "content", "content": {"type": "text", "text": "itinerary"}}],
|
|
"usage": {"total_tokens": 42},
|
|
}
|
|
_KNOWN_DONE_TERMINATOR: Final = b"data: [DONE]\n\n"
|
|
|
|
|
|
class _Interaction(BaseModel):
|
|
id: str
|
|
object: str
|
|
model: str | None = None
|
|
agent: str | None = None
|
|
status: str
|
|
created: str
|
|
steps: list[dict]
|
|
usage: dict
|
|
|
|
|
|
def _sse_event(event_type: str, payload: dict) -> bytes:
|
|
return f"event: {event_type}\ndata: {json.dumps(payload)}\n\n".encode()
|
|
|
|
|
|
def _proxy_stream_event(payload: dict, model: str) -> bytes:
|
|
event: Final = {
|
|
"event_type": payload["event_type"],
|
|
"id": payload["id"],
|
|
**({"object": payload["object"]} if "object" in payload else {}),
|
|
"model": model,
|
|
**{key: value for key, value in payload.items() if key not in {"event_type", "id", "object"}},
|
|
}
|
|
return f"data: {json.dumps(event, separators=(',', ':'))}\n\n".encode()
|
|
|
|
|
|
_STREAM_EVENTS: Final = (
|
|
(
|
|
"interaction.start",
|
|
{
|
|
"event_type": "interaction.start",
|
|
"id": "interaction-wire-2",
|
|
"object": "interaction",
|
|
"status": "in_progress",
|
|
},
|
|
),
|
|
(
|
|
"content.delta",
|
|
{
|
|
"event_type": "content.delta",
|
|
"id": "interaction-wire-2",
|
|
"delta": {"type": "text", "text": "itin"},
|
|
},
|
|
),
|
|
(
|
|
"interaction.complete",
|
|
{
|
|
"event_type": "interaction.complete",
|
|
"id": "interaction-wire-2",
|
|
"object": "interaction",
|
|
"status": "completed",
|
|
"usage": {"total_tokens": 42},
|
|
},
|
|
),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["/v1beta/interactions", "/interactions"])
|
|
@pytest.mark.parametrize(
|
|
"request_input",
|
|
[
|
|
"plan a trip",
|
|
[
|
|
{"role": "user", "content": "plan a trip"},
|
|
{"role": "model", "content": "where to"},
|
|
{"role": "user", "content": "lisbon"},
|
|
],
|
|
],
|
|
ids=["string", "turns"],
|
|
)
|
|
def test_interaction_forwards_native_body_to_gemini(gateway: Gateway, prefix: str, request_input: object) -> None:
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST"
|
|
assert request.target == "/v1beta/interactions"
|
|
assert request.headers["x-goog-api-key"] == _GEMINI_DEPLOYMENT_KEY
|
|
assert request.headers["api-revision"] == "2026-05-20"
|
|
assert json.loads(request.body) == {
|
|
"model": "gemini-2.5-flash",
|
|
"input": request_input,
|
|
"tools": _MODEL_BODY["tools"],
|
|
"system_instruction": "be brief",
|
|
"store": True,
|
|
"background": True,
|
|
"previous_interaction_id": "interaction-prev-123",
|
|
"generation_config": {"temperature": 0.3},
|
|
"response_format": [
|
|
{
|
|
"type": "text",
|
|
"mime_type": "application/json",
|
|
"schema": {"type": "object", "properties": {"plan": {"type": "string"}}},
|
|
},
|
|
{"type": "image", "aspect_ratio": "16:9"},
|
|
],
|
|
}
|
|
return Reply(body=json.dumps(_INTERACTION_REPLY).encode())
|
|
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_GEMINI_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.request(
|
|
"POST", prefix, {"model": model, **_MODEL_BODY, "input": request_input}, key=key
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
payload: Final = _Interaction.model_validate_json(response.content)
|
|
assert payload.model == model, response.text
|
|
assert response.json() == {
|
|
**_INTERACTION_REPLY,
|
|
"model": model,
|
|
"agent": None,
|
|
"updated": None,
|
|
"outputs": None,
|
|
"continuation_token": None,
|
|
}, response.text
|
|
assert [(r.method, r.target) for r in wire.drain()] == [("POST", "/v1beta/interactions")], response.text
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["/v1beta/interactions", "/interactions"])
|
|
def test_interaction_authenticates_google_key_header(gateway: Gateway, prefix: str) -> None:
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST"
|
|
assert request.target == "/v1beta/interactions"
|
|
assert request.headers["x-goog-api-key"] == _GEMINI_DEPLOYMENT_KEY
|
|
assert all(key not in value for value in request.headers.values()), dict(request.headers)
|
|
assert json.loads(request.body) == {
|
|
"model": "gemini-2.5-flash",
|
|
"input": "plan a trip",
|
|
}, request.body.decode()
|
|
return Reply(body=json.dumps(_INTERACTION_REPLY).encode())
|
|
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_GEMINI_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.client.post(
|
|
prefix,
|
|
json={"model": model, "input": "plan a trip"},
|
|
headers={"x-goog-api-key": key},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {
|
|
**_INTERACTION_REPLY,
|
|
"model": model,
|
|
"agent": None,
|
|
"updated": None,
|
|
"outputs": None,
|
|
"continuation_token": None,
|
|
}, response.text
|
|
assert [(r.method, r.target) for r in wire.drain()] == [("POST", "/v1beta/interactions")], response.text
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["/v1beta/interactions", "/interactions"])
|
|
def test_agent_interaction_uses_env_gemini_credentials(gateway: Gateway, tmp_path: Path, prefix: str) -> None:
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST"
|
|
assert request.target == "/v1beta/interactions"
|
|
assert request.headers["x-goog-api-key"] == "synthetic-env-gemini-key"
|
|
assert request.headers["api-revision"] == "2026-05-20"
|
|
body: Final = json.loads(request.body)
|
|
assert body == {
|
|
"agent": "deep-research-pro-preview-12-2025",
|
|
"input": [{"role": "user", "content": "research trails"}],
|
|
}, body
|
|
return Reply(body=json.dumps({**_INTERACTION_REPLY, "agent": "deep-research-pro-preview-12-2025"}).encode())
|
|
|
|
with wire_server(respond) as wire:
|
|
with owned_proxy_process(
|
|
gateway,
|
|
tmp_path,
|
|
{"GEMINI_API_BASE": wire.url, "GEMINI_API_KEY": "synthetic-env-gemini-key"},
|
|
) as owned:
|
|
response: Final = owned.gateway.request(
|
|
"POST",
|
|
prefix,
|
|
{
|
|
"agent": "deep-research-pro-preview-12-2025",
|
|
"input": [{"role": "user", "content": "research trails"}],
|
|
},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
_Interaction.model_validate_json(response.content)
|
|
assert response.json() == {
|
|
**_INTERACTION_REPLY,
|
|
"agent": "deep-research-pro-preview-12-2025",
|
|
"updated": None,
|
|
"outputs": None,
|
|
"continuation_token": None,
|
|
}, response.text
|
|
assert [(r.method, r.target) for r in wire.drain()] == [("POST", "/v1beta/interactions")], response.text
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["/v1beta/interactions", "/interactions"])
|
|
def test_stream_interaction_relays_gemini_events(gateway: Gateway, prefix: str) -> None:
|
|
frames: Final = tuple(_sse_event(event_type, payload) for event_type, payload in _STREAM_EVENTS)
|
|
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST"
|
|
assert request.target == "/v1beta/interactions?alt=sse"
|
|
assert request.headers["x-goog-api-key"] == _GEMINI_DEPLOYMENT_KEY
|
|
body: Final = json.loads(request.body)
|
|
assert body == {
|
|
"model": "gemini-2.5-flash",
|
|
"input": "plan a trip",
|
|
"stream": True,
|
|
}, body
|
|
return Reply(content_type="text/event-stream", chunks=frames)
|
|
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_GEMINI_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.request(
|
|
"POST", prefix, {"model": model, "input": "plan a trip", "stream": True}, key=key
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert response.headers["content-type"].startswith("text/event-stream"), response.headers
|
|
expected: Final = b"".join(_proxy_stream_event(payload, model) for _event_type, payload in _STREAM_EVENTS)
|
|
assert response.content.removesuffix(_KNOWN_DONE_TERMINATOR) == expected, response.text
|
|
assert [(r.method, r.target) for r in wire.drain()] == [("POST", "/v1beta/interactions?alt=sse")], response.text
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["/v1beta/interactions", "/interactions"])
|
|
def test_stream_interaction_ends_without_openai_done_terminator(gateway: Gateway, prefix: str) -> None:
|
|
pytest.skip("BUG: /interactions stream=true appends an OpenAI data: [DONE] frame to the Gemini event stream")
|
|
frames: Final = tuple(_sse_event(event_type, payload) for event_type, payload in _STREAM_EVENTS)
|
|
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST"
|
|
assert request.target == "/v1beta/interactions?alt=sse"
|
|
assert json.loads(request.body) == {
|
|
"model": "gemini-2.5-flash",
|
|
"input": "plan a trip",
|
|
"stream": True,
|
|
}
|
|
return Reply(content_type="text/event-stream", chunks=frames)
|
|
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_GEMINI_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.request(
|
|
"POST", prefix, {"model": model, "input": "plan a trip", "stream": True}, key=key
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert response.headers["content-type"].startswith("text/event-stream"), response.headers
|
|
expected: Final = b"".join(_proxy_stream_event(payload, model) for _event_type, payload in _STREAM_EVENTS)
|
|
assert response.content == expected, response.text
|
|
assert [(r.method, r.target) for r in wire.drain()] == [("POST", "/v1beta/interactions?alt=sse")]
|
|
|
|
|
|
_LONG_DECODE_MODEL: Final = "gemini/gemini-4-argon"
|
|
_LONG_DECODE_BODY: Final = {
|
|
"input": "prove the prime number theorem",
|
|
"generation_config": {"max_output_tokens": 120000, "thinking_level": "high"},
|
|
}
|
|
|
|
|
|
def _hop(hop_id: str, status: str, text: str, token: str | None, input_tokens: int, output_tokens: int) -> dict:
|
|
return {
|
|
"id": hop_id,
|
|
"object": "interaction",
|
|
"model": "gemini-4-argon",
|
|
"status": status,
|
|
"created": "2026-11-05T00:00:00Z",
|
|
"steps": [{"type": "model_output", "content": [{"type": "text", "text": text}]}],
|
|
"usage": {
|
|
"total_input_tokens": input_tokens,
|
|
"total_output_tokens": output_tokens,
|
|
"total_tokens": input_tokens + output_tokens,
|
|
"initial_input_tokens": 79,
|
|
},
|
|
"continuation_token": token,
|
|
}
|
|
|
|
|
|
_FIRST_HOP: Final = _hop("interaction-hop-1", "requires_continuation", "Lemma 1.", "tok-1", 79, 32768)
|
|
_SECOND_HOP: Final = _hop("interaction-hop-2", "completed", " QED.", None, 32847, 1533)
|
|
|
|
|
|
def test_interaction_keeps_requires_continuation_status_and_token_for_the_caller(gateway: Gateway) -> None:
|
|
def respond(request: Request) -> Reply:
|
|
assert request.target == "/v1beta/interactions"
|
|
return Reply(body=json.dumps(_FIRST_HOP).encode())
|
|
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_LONG_DECODE_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.request(
|
|
"POST", "/v1beta/interactions", {"model": model, **_LONG_DECODE_BODY}, key=key
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
sent: Final = [json.loads(r.body) for r in wire.drain()]
|
|
|
|
assert sent == [{"model": "gemini-4-argon", **_LONG_DECODE_BODY}], sent
|
|
payload: Final = response.json()
|
|
assert payload["id"] == "interaction-hop-1", response.text
|
|
assert payload["status"] == "requires_continuation", response.text
|
|
assert payload["continuation_token"] == "tok-1", response.text
|
|
assert payload["usage"]["initial_input_tokens"] == 79, response.text
|
|
|
|
|
|
def test_interaction_forwards_a_caller_continuation_token_to_resume_a_long_decode(gateway: Gateway) -> None:
|
|
def respond(request: Request) -> Reply:
|
|
assert request.target == "/v1beta/interactions"
|
|
return Reply(body=json.dumps(_SECOND_HOP).encode())
|
|
|
|
resume: Final = {
|
|
"generation_config": _LONG_DECODE_BODY["generation_config"],
|
|
"previous_interaction_id": "interaction-hop-1",
|
|
"continuation_token": "tok-1",
|
|
}
|
|
with wire_server(respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=_LONG_DECODE_MODEL, api_base=wire.url, api_key=_GEMINI_DEPLOYMENT_KEY)
|
|
key: Final = scenario.key(models=[model])
|
|
response: Final = gateway.request("POST", "/v1beta/interactions", {"model": model, **resume}, key=key)
|
|
assert response.status_code == 200, response.text
|
|
sent: Final = [json.loads(r.body) for r in wire.drain()]
|
|
|
|
assert sent == [{"model": "gemini-4-argon", **resume}], sent
|
|
payload: Final = response.json()
|
|
assert payload["id"] == "interaction-hop-2", response.text
|
|
assert payload["status"] == "completed", response.text
|
|
assert payload["continuation_token"] is None, response.text
|