Skip to content

Commit

Permalink
feat(api): add request id property to response classes (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Apr 18, 2024
1 parent 8929088 commit 444d680
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@

print(chunk.choices[0].delta.content, end="")
print()

# Response headers:
print("----- custom response headers test -----")
response = client.chat.completions.with_raw_response.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
)
completion = response.parse()
print(response.request_id)
print(completion.choices[0].message.content)
4 changes: 4 additions & 0 deletions src/openai/_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("x-request-id") # type: ignore[no-any-return]

@overload
def parse(self, *, to: type[_T]) -> _T:
...
Expand Down
8 changes: 8 additions & 0 deletions src/openai/_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("x-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("x-request-id") # type: ignore[no-any-return]

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

0 comments on commit 444d680

Please sign in to comment.