MCPcopy Create free account
hub / github.com/anomalyco/opencode-sdk-python / APIResponse

Class APIResponse

src/opencode_ai/_response.py:272–371  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

270
271
272class APIResponse(BaseAPIResponse[R]):
273 @overload
274 def parse(self, *, to: type[_T]) -> _T: ...
275
276 @overload
277 def parse(self) -> R: ...
278
279 def parse(self, *, to: type[_T] | None = None) -> R | _T:
280 """Returns the rich python representation of this response's data.
281
282 For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`.
283
284 You can customise the type that the response is parsed into through
285 the `to` argument, e.g.
286
287 ```py
288 from opencode_ai import BaseModel
289
290
291 class MyModel(BaseModel):
292 foo: str
293
294
295 obj = response.parse(to=MyModel)
296 print(obj.foo)
297 ```
298
299 We support parsing:
300 - `BaseModel`
301 - `dict`
302 - `list`
303 - `Union`
304 - `str`
305 - `int`
306 - `float`
307 - `httpx.Response`
308 """
309 cache_key = to if to is not None else self._cast_to
310 cached = self._parsed_by_type.get(cache_key)
311 if cached is not None:
312 return cached # type: ignore[no-any-return]
313
314 if not self._is_sse_stream:
315 self.read()
316
317 parsed = self._parse(to=to)
318 if is_given(self._options.post_parser):
319 parsed = self._options.post_parser(parsed)
320
321 self._parsed_by_type[cache_key] = parsed
322 return parsed
323
324 def read(self) -> bytes:
325 """Read and return the binary response content."""
326 try:
327 return self.http_response.read()
328 except httpx.StreamConsumed as exc:
329 # The default error raised by httpx isn't very

Calls

no outgoing calls