Implementation:Cohere ai Cohere python HttpResponse
| Knowledge Sources | |
|---|---|
| Domains | SDK, HTTP |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Wraps httpx Response objects to expose both parsed data and HTTP metadata in a generic container.
Description
The http_response module provides three classes: BaseHttpResponse, HttpResponse, and AsyncHttpResponse. BaseHttpResponse is a minimal wrapper around an httpx.Response that exposes headers and status_code as properties. HttpResponse and AsyncHttpResponse extend it with a generic type parameter T to hold parsed response data, and each provides a close method (synchronous and asynchronous respectively) for releasing the underlying connection. These wrappers allow SDK consumers to access both the deserialized API response and raw HTTP-level details from a single object.
Usage
Use HttpResponse[T] when calling synchronous SDK methods that return wrapped responses, and AsyncHttpResponse[T] for their asynchronous counterparts. Access the parsed data via the .data property and HTTP metadata via .headers and .status_code.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/core/http_response.py
Signature
T = TypeVar("T")
class BaseHttpResponse:
_response: httpx.Response
def __init__(self, response: httpx.Response): ...
@property
def headers(self) -> Dict[str, str]: ...
@property
def status_code(self) -> int: ...
class HttpResponse(Generic[T], BaseHttpResponse):
_data: T
def __init__(self, response: httpx.Response, data: T): ...
@property
def data(self) -> T: ...
def close(self) -> None: ...
class AsyncHttpResponse(Generic[T], BaseHttpResponse):
_data: T
def __init__(self, response: httpx.Response, data: T): ...
@property
def data(self) -> T: ...
async def close(self) -> None: ...
Import
from cohere.core.http_response import HttpResponse, AsyncHttpResponse
I/O Contract
Inputs
BaseHttpResponse / HttpResponse / AsyncHttpResponse
| Name | Type | Required | Description |
|---|---|---|---|
| response | httpx.Response | Yes | The raw httpx response object to wrap. |
| data | T | Yes (HttpResponse, AsyncHttpResponse only) | The parsed/deserialized response data of generic type T. |
Outputs
| Name | Type | Description |
|---|---|---|
| headers | Dict[str, str] | Dictionary of HTTP response headers. |
| status_code | int | The HTTP status code of the response. |
| data | T | The deserialized response data (available on HttpResponse and AsyncHttpResponse only). |
| close() / await close() | None | Releases the underlying httpx connection. Use close() for sync and await close() for async. |
Usage Examples
from cohere.core.http_response import HttpResponse, AsyncHttpResponse
import httpx
# Synchronous usage
raw_response = httpx.get("https://api.cohere.com/v2/chat")
parsed_data = {"text": "Hello, world!"}
wrapped = HttpResponse(response=raw_response, data=parsed_data)
print(wrapped.status_code) # 200
print(wrapped.headers) # {"content-type": "application/json", ...}
print(wrapped.data) # {"text": "Hello, world!"}
wrapped.close()
# Asynchronous usage
async def example():
async with httpx.AsyncClient() as client:
raw_response = await client.get("https://api.cohere.com/v2/chat")
parsed_data = {"text": "Hello, world!"}
wrapped = AsyncHttpResponse(response=raw_response, data=parsed_data)
print(wrapped.data)
await wrapped.close()