Skip to content

Commit

Permalink
feat(client): add client close handlers (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jul 28, 2023
1 parent 9a3840d commit f3462a7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ client = Anthropic(

See the httpx documentation for information about the [`proxies`](https://www.python-httpx.org/advanced/#http-proxying) and [`transport`](https://www.python-httpx.org/advanced/#custom-transports) keyword arguments.

## Advanced: Managing HTTP resources

By default we will close the underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__) is called but you can also manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.

## Status

This package is in beta. Its internals and interfaces are not stable and subject to change without a major semver bump;
Expand Down
43 changes: 43 additions & 0 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
import inspect
import platform
from types import TracebackType
from random import random
from typing import (
Any,
Expand Down Expand Up @@ -677,6 +678,27 @@ def __init__(
headers={"Accept": "application/json"},
)

def is_closed(self) -> bool:
return self._client.is_closed

def close(self) -> None:
"""Close the underlying HTTPX client.
The client will *not* be usable after this.
"""
self._client.close()

def __enter__(self: _T) -> _T:
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.close()

@overload
def request(
self,
Expand Down Expand Up @@ -1009,6 +1031,27 @@ def __init__(
headers={"Accept": "application/json"},
)

def is_closed(self) -> bool:
return self._client.is_closed

async def close(self) -> None:
"""Close the underlying HTTPX client.
The client will *not* be usable after this.
"""
await self._client.aclose()

async def __aenter__(self: _T) -> _T:
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
await self.close()

@overload
async def request(
self,
Expand Down
10 changes: 10 additions & 0 deletions src/anthropic/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
import asyncio
from typing import Union, Mapping, Optional

import httpx
Expand Down Expand Up @@ -216,6 +217,9 @@ def copy(
# client.with_options(timeout=10).foo.create(...)
with_options = copy

def __del__(self) -> None:
self.close()

def count_tokens(
self,
text: str,
Expand Down Expand Up @@ -401,6 +405,12 @@ def copy(
# client.with_options(timeout=10).foo.create(...)
with_options = copy

def __del__(self) -> None:
try:
asyncio.get_running_loop().create_task(self.close())
except Exception:
pass

async def count_tokens(
self,
text: str,
Expand Down
34 changes: 34 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import json
import asyncio
import inspect
from typing import Any, Dict, Union, cast

Expand Down Expand Up @@ -355,6 +356,22 @@ def test_base_url_no_trailing_slash(self) -> None:
)
assert request.url == "http://localhost:5000/custom/path/foo"

def test_client_del(self) -> None:
client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
assert not client.is_closed()

client.__del__()

assert client.is_closed()

def test_client_context_manager(self) -> None:
client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
with client as c2:
assert c2 is client
assert not c2.is_closed()
assert not client.is_closed()
assert client.is_closed()

@pytest.mark.respx(base_url=base_url)
def test_default_stream_cls(self, respx_mock: MockRouter) -> None:
class Model(BaseModel):
Expand Down Expand Up @@ -695,6 +712,23 @@ def test_base_url_no_trailing_slash(self) -> None:
)
assert request.url == "http://localhost:5000/custom/path/foo"

async def test_client_del(self) -> None:
client = AsyncAnthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
assert not client.is_closed()

client.__del__()

await asyncio.sleep(0.2)
assert client.is_closed()

async def test_client_context_manager(self) -> None:
client = AsyncAnthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
async with client as c2:
assert c2 is client
assert not c2.is_closed()
assert not client.is_closed()
assert client.is_closed()

@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
async def test_default_stream_cls(self, respx_mock: MockRouter) -> None:
Expand Down

0 comments on commit f3462a7

Please sign in to comment.