Implementation:Googleapis Python genai Interactions Response
| Knowledge Sources | |
|---|---|
| Domains | HTTP_Client, Response_Handling |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for API response wrapper classes providing parsing, streaming, and binary response support for the _interactions subsystem.
Description
The _response module provides APIResponse and AsyncAPIResponse wrappers that handle response deserialization, SSE stream detection, and type-safe parsing. It also provides BinaryAPIResponse for file downloads, StreamedBinaryAPIResponse for streamed downloads, and ResponseContextManager for resource cleanup.
Usage
Internal infrastructure. Users interact with responses indirectly through the API methods that return typed results.
Code Reference
Source Location
- Repository: Googleapis_Python_genai
- File: google/genai/_interactions/_response.py
- Lines: 1-847
Signature
class APIResponse(BaseAPIResponse[R]):
def parse(self, *, to: type[_T] | None = None) -> R | _T: ...
def read(self) -> bytes: ...
def text(self) -> str: ...
def json(self) -> object: ...
def close(self) -> None: ...
class AsyncAPIResponse(BaseAPIResponse[R]):
async def parse(self, *, to: type[_T] | None = None) -> R | _T: ...
async def read(self) -> bytes: ...
class BinaryAPIResponse(APIResponse[bytes]):
def write_to_file(self, file: str | os.PathLike[str]) -> None: ...
class StreamedBinaryAPIResponse(APIResponse[bytes]):
def stream_to_file(self, file: str | os.PathLike[str], *, chunk_size: int | None = None) -> None: ...
Import
from google.genai._interactions._response import APIResponse, AsyncAPIResponse
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| raw | httpx.Response | Yes (constructor) | Raw HTTP response to wrap |
| cast_to | type[R] | Yes (constructor) | Target type for deserialization |
| to | type[_T] | No (parse) | Override target type for custom parsing |
Outputs
| Name | Type | Description |
|---|---|---|
| parse() returns | R or _T | Deserialized typed response |
| read() returns | bytes | Raw response body bytes |
| text() returns | str | Response body as text |
| json() returns | object | Parsed JSON response body |
Usage Examples
# Internal usage pattern
# APIResponse wraps httpx responses and handles type-safe parsing
response = client.with_raw_response.interactions.create(...)
data = response.parse() # Returns typed Interaction object
print(response.status_code)
print(response.headers)