Skip to content

Commit

Permalink
feat(client): make request-id header more accessible (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Jul 10, 2024
1 parent ade509f commit beba471
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ def _request(
response.reason_phrase,
response.headers,
)
log.debug("request_id: %s", response.headers.get("request-id"))

try:
response.raise_for_status()
Expand Down
2 changes: 2 additions & 0 deletions src/anthropic/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ class APIStatusError(APIError):

response: httpx.Response
status_code: int
request_id: str | None

def __init__(self, message: str, *, response: httpx.Response, body: object | None) -> None:
super().__init__(message, response.request, body=body)
self.response = response
self.status_code = response.status_code
self.request_id = response.headers.get("request-id")


class APIConnectionError(APIError):
Expand Down
4 changes: 4 additions & 0 deletions src/anthropic/_legacy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(
self._options = options
self.http_response = raw

@property
def request_id(self) -> str | None:
return self.http_response.headers.get("request-id") # type: ignore[no-any-return]

@overload
def parse(self, *, to: type[_T]) -> _T:
...
Expand Down
8 changes: 8 additions & 0 deletions src/anthropic/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:


class APIResponse(BaseAPIResponse[R]):
@property
def request_id(self) -> str | None:
return self.http_response.headers.get("request-id") # type: ignore[no-any-return]

@overload
def parse(self, *, to: type[_T]) -> _T:
...
Expand Down Expand Up @@ -362,6 +366,10 @@ def iter_lines(self) -> Iterator[str]:


class AsyncAPIResponse(BaseAPIResponse[R]):
@property
def request_id(self) -> str | None:
return self.http_response.headers.get("request-id") # type: ignore[no-any-return]

@overload
async def parse(self, *, to: type[_T]) -> _T:
...
Expand Down

0 comments on commit beba471

Please sign in to comment.