| 384 | |
| 385 | |
| 386 | class AsyncAPIResponse(BaseAPIResponse[R]): |
| 387 | @property |
| 388 | def request_id(self) -> str | None: |
| 389 | return self.http_response.headers.get(class="st">"x-request-id") class="cm"># type: ignore[no-any-return] |
| 390 | |
| 391 | @overload |
| 392 | async def parse(self, *, to: type[_T]) -> _T: ... |
| 393 | |
| 394 | @overload |
| 395 | async def parse(self) -> R: ... |
| 396 | |
| 397 | async def parse(self, *, to: type[_T] | None = None) -> R | _T: |
| 398 | class="st">"""Returns the rich python representation of this response&class="cm">#x27;s data. |
| 399 | |
| 400 | For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`. |
| 401 | |
| 402 | You can customise the type that the response is parsed into through |
| 403 | the `to` argument, e.g. |
| 404 | |
| 405 | ```py |
| 406 | from openai import BaseModel |
| 407 | |
| 408 | |
| 409 | class MyModel(BaseModel): |
| 410 | foo: str |
| 411 | |
| 412 | |
| 413 | obj = response.parse(to=MyModel) |
| 414 | print(obj.foo) |
| 415 | ``` |
| 416 | |
| 417 | We support parsing: |
| 418 | - `BaseModel` |
| 419 | - `dict` |
| 420 | - `list` |
| 421 | - `Union` |
| 422 | - `str` |
| 423 | - `httpx.Response` |
| 424 | class="st">""" |
| 425 | cache_key = to if to is not None else self._cast_to |
| 426 | cached = self._parsed_by_type.get(cache_key) |
| 427 | if cached is not None: |
| 428 | return cached class="cm"># type: ignore[no-any-return] |
| 429 | |
| 430 | if not self._is_sse_stream: |
| 431 | await self.read() |
| 432 | |
| 433 | parsed = self._parse(to=to) |
| 434 | if is_given(self._options.post_parser): |
| 435 | parsed = self._options.post_parser(parsed) |
| 436 | |
| 437 | if isinstance(parsed, BaseModel): |
| 438 | add_request_id(parsed, self.request_id) |
| 439 | |
| 440 | self._parsed_by_type[cache_key] = parsed |
| 441 | return cast(R, parsed) |
| 442 | |
| 443 | async def read(self) -> bytes: |
no outgoing calls