litellm/litellm/interactions/http_handler.py
zoveki 8973e2dc4f
feat(vertex_ai): label Vertex requests with the caller's key, team, org, and project (#50732)
* feat(vertex_ai): label Vertex requests with the caller's key, team, org, and project

Adds the opt-in litellm.vertex_request_label_fields setting, the Vertex twin of bedrock_request_metadata_fields. When set, Gemini, embeddings, Imagen and Gemini image generation, rerank, and Claude on Vertex (chat and /v1/messages) send the listed proxy identity fields and spend_logs_metadata as request labels, identity first, with the user_api_key_ prefix reserved so a caller cannot forge or evict them. Identity text is rewritten to fit Google's label rules. Gemini image generation now forwards labels at all.

* feat(vertex_ai): send the opt-in identity labels on multimodal embeddings, the Google GenAI route, and batches

With litellm_settings.vertex_request_label_fields on, the multimodal embedding
predict body, the native generateContent and streamGenerateContent body, and the
BatchPredictionJob now carry the key's identity labels the same way chat does

Caller labels on GenAI are resolved after the extra_body merge, so neither a
top-level labels field nor extra_body.labels can write the reserved prefix. Batch
labels come only from the proxy's litellm_metadata, never the caller's OpenAI
batch metadata. With the setting off every route sends the same body as before

* fix(vertex_ai): keep identity labels when a Gemini caller sends extra_body labels

* fix(vertex_ai): label Interactions, image edits, Mistral, and the /vertex_ai pass-through, and gate Gemini image labels on the setting

* fix(vertex_ai): move body labels into the labels header on Vertex rawPredict pass-through calls

* fix(vertex_ai): send Mistral extra_body labels in the labels header instead of the body

Labels a caller nests in extra_body now merge over top-level labels before the
X-Vertex-AI-Labels header is built, the way the Gemini body merges them, and
leave the outbound body. The token-fetch stubs in the Mistral and multimodal
embedding tests are scoped to their instance so later tests see the real method

* fix(vertex_ai): limit pass-through labels to POST and to publishers that take the labels header

rawPredict to custom endpoints and publishers other than Anthropic and Mistral, and PATCH or PUT bodies, now reach Vertex as the caller sent them. The pass-through drops a caller's labels header where it writes body labels, a single field written as text turns the setting on for that field, and an undecodable Claude labels header falls back to the caller's labels

* fix(vertex): label Veo video, Gemini transcription, and pass-through predictLongRunning requests

* fix(vertex): read the pass-through publisher from the end of the upstream path

A project id named publishers shifted the first-match lookup, so a forged X-Vertex-AI-Labels header reached Claude or Mistral untouched

* fix(vertex): drop a caller labels header on body-labelled Vertex routes while identity labels are on

* test(vertex): script the v1beta1 GenAI target in the labels header integration test

* test(vertex): type the transcription test's access token override

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-14 00:39:01 -05:00

707 lines
25 KiB
Python

"""
HTTP Handler for Interactions API requests.
This module handles the HTTP communication for the Google Interactions API.
"""
from collections.abc import AsyncIterator, Callable, Coroutine, Iterator, Mapping
from typing import Final, Protocol
import httpx
import litellm
from litellm.constants import request_timeout
from litellm.interactions.streaming_iterator import (
InteractionsAPIStreamingIterator,
SyncInteractionsAPIStreamingIterator,
)
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms.base_llm.interactions.transformation import BaseInteractionsAPIConfig
from litellm.llms.custom_httpx.http_handler import (
AsyncHTTPHandler,
HTTPHandler,
get_async_httpx_client,
get_httpx_client,
)
from litellm.types.interactions import (
CancelInteractionResult,
DeleteInteractionResult,
InteractionInput,
InteractionsAPIOptionalRequestParams,
InteractionsAPIResponse,
InteractionsAPIStreamingResponse,
)
from litellm.types.router import GenericLiteLLMParams
class _ErrorClassSource(Protocol):
@property
def get_error_class(self) -> Callable[..., Exception]: ...
class _BaseHTTPHandler:
"""
Shared HTTP infrastructure for LiteLLM handler classes.
Provides common client resolution and error-mapping helpers so that
handler subclasses (InteractionsHTTPHandler, AgentsHTTPHandler, …) do
not duplicate this boilerplate.
"""
def _handle_error(self, e: Exception, provider_config: _ErrorClassSource) -> Exception:
if isinstance(e, httpx.HTTPStatusError):
return provider_config.get_error_class(
error_message=e.response.text,
status_code=e.response.status_code,
headers=dict(e.response.headers),
)
return e
def _sync_client(
self,
litellm_params: GenericLiteLLMParams,
client: HTTPHandler | None,
) -> HTTPHandler:
return client or get_httpx_client(params={"ssl_verify": litellm_params.get("ssl_verify", None)})
def _async_client(
self,
litellm_params: GenericLiteLLMParams,
client: AsyncHTTPHandler | None,
) -> AsyncHTTPHandler:
# GenericLiteLLMParams.get uses getattr; an unset field is None, not the default.
custom_llm_provider: Final = litellm_params.get("custom_llm_provider") or "gemini"
return client or get_async_httpx_client(
llm_provider=litellm.LlmProviders(custom_llm_provider),
params={"ssl_verify": litellm_params.get("ssl_verify", None)},
)
class InteractionsHTTPHandler(_BaseHTTPHandler):
"""
HTTP handler for Interactions API requests.
"""
# _handle_error is inherited from _BaseHTTPHandler (accepts any config exposing get_error_class).
# AgentsHTTPHandler also extends this class and passes BaseAgentsAPIConfig, which
# is structurally compatible but a different type — keeping the override here with
# BaseInteractionsAPIConfig would cause type errors in the subclass.
# =========================================================
# CREATE INTERACTION
# =========================================================
def create_interaction(
self,
interactions_api_config: BaseInteractionsAPIConfig,
optional_params: InteractionsAPIOptionalRequestParams,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
model: str | None = None,
agent: str | None = None,
input: InteractionInput | None = None,
extra_headers: dict[str, str] | None = None,
extra_body: Mapping[str, object] | None = None,
timeout: float | httpx.Timeout | None = None,
client: HTTPHandler | None = None,
_is_async: bool = False,
stream: bool | None = None,
) -> (
InteractionsAPIResponse
| Iterator[InteractionsAPIStreamingResponse]
| Coroutine[object, object, InteractionsAPIResponse | AsyncIterator[InteractionsAPIStreamingResponse]]
):
"""
Create a new interaction (synchronous or async based on _is_async flag).
Per Google's OpenAPI spec, the endpoint is POST /{api_version}/interactions
"""
if _is_async:
return self.async_create_interaction(
model=model,
agent=agent,
input=input,
interactions_api_config=interactions_api_config,
optional_params=optional_params,
custom_llm_provider=custom_llm_provider,
litellm_params=litellm_params,
logging_obj=logging_obj,
extra_headers=extra_headers,
extra_body=extra_body,
timeout=timeout,
stream=stream,
)
if client is None:
sync_httpx_client = get_httpx_client(params={"ssl_verify": litellm_params.get("ssl_verify", None)})
else:
sync_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model=model or "",
litellm_params=litellm_params,
)
api_base: Final = interactions_api_config.get_complete_url(
api_base=litellm_params.api_base or "",
model=model,
agent=agent,
litellm_params=dict(litellm_params),
stream=stream,
)
data: Final = interactions_api_config.transform_request(
model=model,
agent=agent,
input=input,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
)
request_fields: Final = interactions_api_config.transform_top_level_request_fields(extra_body, litellm_params)
if request_fields:
data.update(request_fields)
# Logging
logging_obj.pre_call(
input=input,
api_key="",
additional_args={
"complete_input_dict": data,
"api_base": api_base,
"headers": headers,
},
)
try:
if stream:
response = sync_httpx_client.post(
url=api_base,
headers=headers,
json=data,
timeout=timeout or request_timeout,
stream=True,
)
return self._create_sync_streaming_iterator(
response=response,
model=model,
logging_obj=logging_obj,
interactions_api_config=interactions_api_config,
)
else:
response = sync_httpx_client.post(
url=api_base,
headers=headers,
json=data,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_response(
model=model,
raw_response=response,
logging_obj=logging_obj,
)
async def async_create_interaction(
self,
interactions_api_config: BaseInteractionsAPIConfig,
optional_params: InteractionsAPIOptionalRequestParams,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
model: str | None = None,
agent: str | None = None,
input: InteractionInput | None = None,
extra_headers: dict[str, str] | None = None,
extra_body: Mapping[str, object] | None = None,
timeout: float | httpx.Timeout | None = None,
client: AsyncHTTPHandler | None = None,
stream: bool | None = None,
) -> InteractionsAPIResponse | AsyncIterator[InteractionsAPIStreamingResponse]:
"""
Create a new interaction (async version).
"""
if client is None:
async_httpx_client = get_async_httpx_client(
llm_provider=litellm.LlmProviders(custom_llm_provider),
params={"ssl_verify": litellm_params.get("ssl_verify", None)},
)
else:
async_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model=model or "",
litellm_params=litellm_params,
)
api_base: Final = interactions_api_config.get_complete_url(
api_base=litellm_params.api_base or "",
model=model,
agent=agent,
litellm_params=dict(litellm_params),
stream=stream,
)
data: Final = interactions_api_config.transform_request(
model=model,
agent=agent,
input=input,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
)
request_fields: Final = interactions_api_config.transform_top_level_request_fields(extra_body, litellm_params)
if request_fields:
data.update(request_fields)
# Logging
logging_obj.pre_call(
input=input,
api_key="",
additional_args={
"complete_input_dict": data,
"api_base": api_base,
"headers": headers,
},
)
try:
if stream:
response = await async_httpx_client.post(
url=api_base,
headers=headers,
json=data,
timeout=timeout or request_timeout,
stream=True,
)
return self._create_async_streaming_iterator(
response=response,
model=model,
logging_obj=logging_obj,
interactions_api_config=interactions_api_config,
)
else:
response = await async_httpx_client.post(
url=api_base,
headers=headers,
json=data,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_response(
model=model,
raw_response=response,
logging_obj=logging_obj,
)
def _create_sync_streaming_iterator(
self,
response: httpx.Response,
model: str | None,
logging_obj: LiteLLMLoggingObj,
interactions_api_config: BaseInteractionsAPIConfig,
) -> SyncInteractionsAPIStreamingIterator:
"""Create a synchronous streaming iterator.
Google AI's streaming format uses SSE (Server-Sent Events).
Returns a proper streaming iterator that yields chunks as they arrive.
"""
return SyncInteractionsAPIStreamingIterator(
response=response,
model=model,
interactions_api_config=interactions_api_config,
logging_obj=logging_obj,
)
def _create_async_streaming_iterator(
self,
response: httpx.Response,
model: str | None,
logging_obj: LiteLLMLoggingObj,
interactions_api_config: BaseInteractionsAPIConfig,
) -> InteractionsAPIStreamingIterator:
"""Create an asynchronous streaming iterator.
Google AI's streaming format uses SSE (Server-Sent Events).
Returns a proper streaming iterator that yields chunks as they arrive.
"""
return InteractionsAPIStreamingIterator(
response=response,
model=model,
interactions_api_config=interactions_api_config,
logging_obj=logging_obj,
)
# =========================================================
# GET INTERACTION
# =========================================================
def get_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: HTTPHandler | None = None,
_is_async: bool = False,
) -> InteractionsAPIResponse | Coroutine[object, object, InteractionsAPIResponse]:
"""Get an interaction by ID."""
if _is_async:
return self.async_get_interaction(
interaction_id=interaction_id,
interactions_api_config=interactions_api_config,
custom_llm_provider=custom_llm_provider,
litellm_params=litellm_params,
logging_obj=logging_obj,
extra_headers=extra_headers,
timeout=timeout,
)
if client is None:
sync_httpx_client = get_httpx_client(params={"ssl_verify": litellm_params.get("ssl_verify", None)})
else:
sync_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, params = interactions_api_config.transform_get_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = sync_httpx_client.get(
url=url,
headers=headers,
params=params,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_get_interaction_response(
raw_response=response,
logging_obj=logging_obj,
)
async def async_get_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: AsyncHTTPHandler | None = None,
) -> InteractionsAPIResponse:
"""Get an interaction by ID (async version)."""
if client is None:
async_httpx_client = get_async_httpx_client(
llm_provider=litellm.LlmProviders(custom_llm_provider),
params={"ssl_verify": litellm_params.get("ssl_verify", None)},
)
else:
async_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, params = interactions_api_config.transform_get_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = await async_httpx_client.get(
url=url,
headers=headers,
params=params,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_get_interaction_response(
raw_response=response,
logging_obj=logging_obj,
)
# =========================================================
# DELETE INTERACTION
# =========================================================
def delete_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: HTTPHandler | None = None,
_is_async: bool = False,
) -> DeleteInteractionResult | Coroutine[object, object, DeleteInteractionResult]:
"""Delete an interaction by ID."""
if _is_async:
return self.async_delete_interaction(
interaction_id=interaction_id,
interactions_api_config=interactions_api_config,
custom_llm_provider=custom_llm_provider,
litellm_params=litellm_params,
logging_obj=logging_obj,
extra_headers=extra_headers,
timeout=timeout,
)
if client is None:
sync_httpx_client = get_httpx_client(params={"ssl_verify": litellm_params.get("ssl_verify", None)})
else:
sync_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, data = interactions_api_config.transform_delete_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = sync_httpx_client.delete(
url=url,
headers=headers,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_delete_interaction_response(
raw_response=response,
logging_obj=logging_obj,
interaction_id=interaction_id,
)
async def async_delete_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: AsyncHTTPHandler | None = None,
) -> DeleteInteractionResult:
"""Delete an interaction by ID (async version)."""
if client is None:
async_httpx_client = get_async_httpx_client(
llm_provider=litellm.LlmProviders(custom_llm_provider),
params={"ssl_verify": litellm_params.get("ssl_verify", None)},
)
else:
async_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, data = interactions_api_config.transform_delete_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = await async_httpx_client.delete(
url=url,
headers=headers,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_delete_interaction_response(
raw_response=response,
logging_obj=logging_obj,
interaction_id=interaction_id,
)
# =========================================================
# CANCEL INTERACTION
# =========================================================
def cancel_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: HTTPHandler | None = None,
_is_async: bool = False,
) -> CancelInteractionResult | Coroutine[object, object, CancelInteractionResult]:
"""Cancel an interaction by ID."""
if _is_async:
return self.async_cancel_interaction(
interaction_id=interaction_id,
interactions_api_config=interactions_api_config,
custom_llm_provider=custom_llm_provider,
litellm_params=litellm_params,
logging_obj=logging_obj,
extra_headers=extra_headers,
timeout=timeout,
)
if client is None:
sync_httpx_client = get_httpx_client(params={"ssl_verify": litellm_params.get("ssl_verify", None)})
else:
sync_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, data = interactions_api_config.transform_cancel_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = sync_httpx_client.post(
url=url,
headers=headers,
json=data,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_cancel_interaction_response(
raw_response=response,
logging_obj=logging_obj,
)
async def async_cancel_interaction(
self,
interaction_id: str,
interactions_api_config: BaseInteractionsAPIConfig,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
logging_obj: LiteLLMLoggingObj,
extra_headers: dict[str, str] | None = None,
timeout: float | httpx.Timeout | None = None,
client: AsyncHTTPHandler | None = None,
) -> CancelInteractionResult:
"""Cancel an interaction by ID (async version)."""
if client is None:
async_httpx_client = get_async_httpx_client(
llm_provider=litellm.LlmProviders(custom_llm_provider),
params={"ssl_verify": litellm_params.get("ssl_verify", None)},
)
else:
async_httpx_client = client
headers: Final = interactions_api_config.validate_environment(
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
url, data = interactions_api_config.transform_cancel_interaction_request(
interaction_id=interaction_id,
api_base=litellm_params.api_base or "",
litellm_params=litellm_params,
headers=headers,
)
logging_obj.pre_call(
input=interaction_id,
api_key="",
additional_args={"api_base": url, "headers": headers},
)
try:
response: Final = await async_httpx_client.post(
url=url,
headers=headers,
json=data,
timeout=timeout or request_timeout,
)
except Exception as e:
raise self._handle_error(e=e, provider_config=interactions_api_config)
return interactions_api_config.transform_cancel_interaction_response(
raw_response=response,
logging_obj=logging_obj,
)
# Initialize the HTTP handler singleton
interactions_http_handler: Final = InteractionsHTTPHandler()