Skip to content

Commit 444d680

Browse files
feat(api): add request id property to response classes (#1341)
1 parent 8929088 commit 444d680

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

examples/demo.py

+15
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,18 @@
3636

3737
print(chunk.choices[0].delta.content, end="")
3838
print()
39+
40+
# Response headers:
41+
print("----- custom response headers test -----")
42+
response = client.chat.completions.with_raw_response.create(
43+
model="gpt-4",
44+
messages=[
45+
{
46+
"role": "user",
47+
"content": "Say this is a test",
48+
}
49+
],
50+
)
51+
completion = response.parse()
52+
print(response.request_id)
53+
print(completion.choices[0].message.content)

src/openai/_legacy_response.py

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def __init__(
7171
self._options = options
7272
self.http_response = raw
7373

74+
@property
75+
def request_id(self) -> str | None:
76+
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]
77+
7478
@overload
7579
def parse(self, *, to: type[_T]) -> _T:
7680
...

src/openai/_response.py

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
258258

259259

260260
class APIResponse(BaseAPIResponse[R]):
261+
@property
262+
def request_id(self) -> str | None:
263+
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]
264+
261265
@overload
262266
def parse(self, *, to: type[_T]) -> _T:
263267
...
@@ -362,6 +366,10 @@ def iter_lines(self) -> Iterator[str]:
362366

363367

364368
class AsyncAPIResponse(BaseAPIResponse[R]):
369+
@property
370+
def request_id(self) -> str | None:
371+
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]
372+
365373
@overload
366374
async def parse(self, *, to: type[_T]) -> _T:
367375
...

0 commit comments

Comments
 (0)