One change
For Amazon Bedrock support
cli-sdks-libraries/sdks/python
Nearest release: v2.1.239, published 2 hours before this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
cli-sdks-libraries/sdks/python Changed · +20 / -18 lines
from line 36
## Requirements -Python 3.9 or later is required. +Python 3.10 or later is required. If you are upgrading from a 0.x release of the SDK, see the [v1 migration guide](https://github.com/anthropics/anthropic-sdk-python/blob/main/MIGRATION.md) for the list of breaking changes. ## Usage
from line 102
### Using aiohttp for better concurrency -For improved async performance, you can use the `aiohttp` HTTP backend instead of the default `httpx`: +For improved async performance, you can use the `aiohttp` HTTP backend instead of the default `httpx2`: ```python import os
from line 362
) except anthropic.APIConnectionError as e: print("The server could not be reached") - print(e.__cause__) # an underlying Exception, likely raised within httpx + print(e.__cause__) # an underlying Exception, likely raised within httpx2 except anthropic.RateLimitError as e: print("A 429 status code was received; we should back off a bit.") except anthropic.APIStatusError as e:
from line 426
## Timeouts -By default requests time out after 10 minutes. You can configure this with a `timeout` option, which accepts a float or an `httpx.Timeout` object: +By default requests time out after 10 minutes. You can configure this with a `timeout` option, which accepts a float or an `httpx2.Timeout` object: ```python -import httpx +import httpx2 from anthropic import Anthropic # Configure the default for all requests:
from line 439
# More granular control: client = Anthropic( - timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0), + timeout=httpx2.Timeout(60.0, read=5.0, write=10.0, connect=2.0), ) # Override per-request:
from line 588
### Accessing raw response data (for example, headers) -The "raw" `Response` returned by `httpx` can be accessed through the `.with_raw_response` property on the client. This is useful for accessing response headers or other metadata: +The "raw" `Response` returned by `httpx2` can be accessed through the `.with_raw_response` property on the client. This is useful for accessing response headers or other metadata: ```python client = Anthropic()
from line 606
print(message.content) ``` -These methods return an `APIResponse` object. +These methods return an `APIResponse` object. On the async client they return an `AsyncAPIResponse`, and `.parse()`, `.read()`, `.text()`, and `.json()` must be awaited. ### Streaming response body
from line 645
To make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs. Options on the client, such as retries, are respected when making these requests. ```python -import httpx +import httpx2 response = client.post( "/foo", - cast_to=httpx.Response, + cast_to=httpx2.Response, body={"my_param": True}, )
from line 670
### Configuring the HTTP client -You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including support for proxies and transports: +The SDK sends requests with [httpx2](https://httpx2.pydantic.dev), an API-compatible fork of `httpx`. To customize the HTTP client, including proxies and transports, pass your own [httpx2 client](https://httpx2.pydantic.dev/api/#client) as `http_client`: ```python -import httpx +import httpx2 from anthropic import Anthropic, DefaultHttpxClient client = Anthropic(
from line 681
base_url="http://my.test.server.example.com:8083", http_client=DefaultHttpxClient( proxy="http://my.test.proxy.example.com", - transport=httpx.HTTPTransport(local_address="0.0.0.0"), + transport=httpx2.HTTPTransport(local_address="0.0.0.0"), ), ) ```
from line 693
``` <Note> - Use `DefaultHttpxClient` and `DefaultAsyncHttpxClient` instead of raw `httpx.Client` and `httpx.AsyncClient` to ensure the SDK's default configuration (such as timeouts and connection limits) is preserved. + Use `DefaultHttpxClient` and `DefaultAsyncHttpxClient` instead of raw `httpx2.Client` and `httpx2.AsyncClient` to ensure the SDK's default configuration (such as timeouts and connection limits) is preserved. The `http_client` argument must be an `httpx2` client. Passing a client from the separate `httpx` package raises a `TypeError`. </Note> + +Tracing and mocking tools that patch `httpx` itself, such as OpenTelemetry's `HTTPXClientInstrumentor`, Sentry's `httpx` integration, `respx`, or `pytest-httpx`, do not see the SDK's requests by default. To use them, call `httpx2.alias_httpx()` once at startup, before anything imports `httpx`. This makes `import httpx` resolve to `httpx2` for the whole process. ### Managing HTTP resources