MCPcopy
hub / github.com/openai/openai-python / APIResponse

Class APIResponse

src/openai/_response.py:277–383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

275
276
277class APIResponse(BaseAPIResponse[R]):
278 @property
279 def request_id(self) -> str | None:
280 return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]
281
282 @overload
283 def parse(self, *, to: type[_T]) -> _T: ...
284
285 @overload
286 def parse(self) -> R: ...
287
288 def parse(self, *, to: type[_T] | None = None) -> R | _T:
289 """Returns the rich python representation of this response's data.
290
291 For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`.
292
293 You can customise the type that the response is parsed into through
294 the `to` argument, e.g.
295
296 ```py
297 from openai import BaseModel
298
299
300 class MyModel(BaseModel):
301 foo: str
302
303
304 obj = response.parse(to=MyModel)
305 print(obj.foo)
306 ```
307
308 We support parsing:
309 - `BaseModel`
310 - `dict`
311 - `list`
312 - `Union`
313 - `str`
314 - `int`
315 - `float`
316 - `httpx.Response`
317 """
318 cache_key = to if to is not None else self._cast_to
319 cached = self._parsed_by_type.get(cache_key)
320 if cached is not None:
321 return cached # type: ignore[no-any-return]
322
323 if not self._is_sse_stream:
324 self.read()
325
326 parsed = self._parse(to=to)
327 if is_given(self._options.post_parser):
328 parsed = self._options.post_parser(parsed)
329
330 if isinstance(parsed, BaseModel):
331 add_request_id(parsed, self.request_id)
332
333 self._parsed_by_type[cache_key] = parsed
334 return cast(R, parsed)

Calls

no outgoing calls