utils

Utility functions for Google services.

pipecat.services.google.utils.update_google_client_http_options(http_options: dict[str, Any] | Any | None) → Any[source]

Updates http_options with the x-goog-api-client header.

Parameters:

http_options – The existing http_options, which can be None, a dictionary, or an object with a ‘headers’ attribute.

Returns:

The updated http_options.

pipecat.services.google.utils.GEMINI_API_VERSION = 'v1beta'

The Gemini Developer API version clients use unless their http_options name one. google-genai 3 defaults to v1, which doesn’t serve the Gemini Live models or preview models such as the default TTS model.

pipecat.services.google.utils.gemini_api_http_options(http_options: dict[str, Any] | Any | None) → Any[source]

The http_options for a Gemini Developer API client, on v1beta unless they name a version.

When GOOGLE_GENAI_USE_VERTEXAI or GOOGLE_GENAI_USE_ENTERPRISE makes google-genai build a Vertex AI client instead (express mode, with an API key), the http_options are returned as given, so the client keeps the SDK’s Vertex default: Vertex AI serves no v1beta.

Parameters:

http_options – The client’s http_options: None, a dictionary, or an object with an api_version attribute, which is set in place.

Returns:

The http_options with an API version, or as given for a Vertex AI client.

async pipecat.services.google.utils.stream_with_retry(open_stream: ~collections.abc.Callable[[], ~collections.abc.Awaitable[~collections.abc.AsyncIterator[~pipecat.services.google.utils.T]]], *, idle_timeout: float | None, retry_timeout: float | None, retry_on_timeout: bool, timeout_errors: tuple[type[BaseException], ...] = (<class 'TimeoutError'>,), label: str = '') → AsyncGenerator[T, None][source]

Yield a streamed response’s chunks, re-issuing the request once if it stalls.

A request that is accepted and then produces nothing shows up as a first chunk that never arrives. Re-issuing is safe only up to that point: once a chunk has been yielded, its content is already on its way downstream.

Parameters:
  • open_stream – Sends the request and returns its streamed response.

  • idle_timeout – How long to wait for each chunk, and for the first one when the request isn’t retried. None waits indefinitely.

  • retry_timeout – How long to wait for the first chunk before re-issuing the request, when retry_on_timeout is set.

  • retry_on_timeout – Whether to re-issue the request once if its first chunk doesn’t arrive within retry_timeout.

  • timeout_errors – The errors that mean the first chunk timed out.

  • label – Names the stream’s owner in the retry’s log line.

Yields:

Each chunk of the streamed response.

Raises:

TimeoutError – If the stream stalls past the timeout it is bounded by.

async pipecat.services.google.utils.iter_with_timeouts(response: AsyncIterator[T], *, first_timeout: float | None, idle_timeout: float | None) → AsyncGenerator[T, None][source]

Yield streamed chunks, giving up if the stream stalls between them.

A stream that stops producing without closing would otherwise leave the response open indefinitely, since the API clients apply no timeout of their own. The timeout covers the gap between chunks rather than the response as a whole, so a slow but healthy stream is never cut short.

Parameters:
  • response – The streamed response to consume.

  • first_timeout – How long to wait for the first chunk.

  • idle_timeout – How long to wait for every later chunk.

Yields:

Each chunk of the streamed response.

Raises:

TimeoutError – If the next chunk doesn’t arrive in time.

async pipecat.services.google.utils.close_stream(stream: Any) → None[source]

Close a streamed response, releasing its HTTP resources.

Closing is best-effort: generateContent’s stream type only promises AsyncIterator, which has no aclose(), and the Interactions API’s stream closes with close(), which leaves the event generator it reads from open. That generator is closed first, so it is finalized here rather than by the event loop’s async generator hooks, which crash under uvloop on Python 3.12+ (MagicStack/uvloop#699).

Parameters:

stream – The streamed response.