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

Method _parse

src/openai/_legacy_response.py:197–341  ·  view source on GitHub ↗
(self, *, to: type[_T] | None = None)

Source from the content-addressed store, hash-verified

195 return self.http_response.elapsed
196
197 def _parse(self, *, to: type[_T] | None = None) -> R | _T:
198 cast_to = to if to is not None else self._cast_to
199
200 # unwrap `TypeAlias('Name', T)` -> `T`
201 if is_type_alias_type(cast_to):
202 cast_to = cast_to.__value__ # type: ignore[unreachable]
203
204 # unwrap `Annotated[T, ...]` -> `T`
205 if cast_to and is_annotated_type(cast_to):
206 cast_to = extract_type_arg(cast_to, 0)
207
208 origin = get_origin(cast_to) or cast_to
209
210 if self._stream:
211 if to:
212 if not is_stream_class_type(to):
213 raise TypeError(f"Expected custom parse type to be a subclass of {Stream} or {AsyncStream}")
214
215 return cast(
216 _T,
217 to(
218 cast_to=extract_stream_chunk_type(
219 to,
220 failure_message="Expected custom stream type to be passed with a type argument, e.g. Stream[ChunkType]",
221 ),
222 response=self.http_response,
223 client=cast(Any, self._client),
224 options=self._options,
225 ),
226 )
227
228 if self._stream_cls:
229 return cast(
230 R,
231 self._stream_cls(
232 cast_to=extract_stream_chunk_type(self._stream_cls),
233 response=self.http_response,
234 client=cast(Any, self._client),
235 options=self._options,
236 ),
237 )
238
239 stream_cls = cast("type[Stream[Any]] | type[AsyncStream[Any]] | None", self._client._default_stream_cls)
240 if stream_cls is None:
241 raise MissingStreamClassError()
242
243 return cast(
244 R,
245 stream_cls(
246 cast_to=cast_to,
247 response=self.http_response,
248 client=cast(Any, self._client),
249 options=self._options,
250 ),
251 )
252
253 if cast_to is NoneType:
254 return cast(R, None)

Callers 1

parseMethod · 0.95

Calls 12

is_type_alias_typeFunction · 0.85
is_annotated_typeFunction · 0.85
extract_type_argFunction · 0.85
is_stream_class_typeFunction · 0.85
is_basemodelFunction · 0.85
get_originFunction · 0.70
getMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected