MCPcopy Index your code
hub / github.com/OpenBMB/ChatDev / _annotation_to_schema

Function _annotation_to_schema

utils/function_catalog.py:249–278  ·  view source on GitHub ↗
(annotation: Any)

Source from the content-addressed store, hash-verified

247
248
249def _annotation_to_schema(annotation: Any) -> Dict[str, Any]:
250 if annotation is inspect._empty or annotation is Any:
251 return {"type": "string"}
252
253 origin = get_origin(annotation)
254 if origin is None:
255 return _primitive_schema(annotation)
256
257 if origin is list or origin is List or origin is abc.Sequence or origin is abc.MutableSequence:
258 item_annotation = get_args(annotation)[0] if get_args(annotation) else Any
259 return {
260 "type": "array",
261 "items": _annotation_to_schema(item_annotation),
262 }
263
264 if origin in {dict, Dict, abc.Mapping, abc.MutableMapping}:
265 return {"type": "object"}
266
267 if origin is Union:
268 literals = [arg for arg in get_args(annotation) if arg is not type(None)] # noqa: E721
269 literal_schema = _try_literal_schema(literals)
270 if literal_schema:
271 return literal_schema
272 return {"type": "string"}
273
274 if origin is Literal:
275 values = list(get_args(annotation))
276 return _literal_schema(values)
277
278 return {"type": "string"}
279
280
281def _primitive_schema(annotation: Any) -> Dict[str, Any]:

Callers 1

_build_parameters_schemaFunction · 0.85

Calls 3

_primitive_schemaFunction · 0.85
_try_literal_schemaFunction · 0.85
_literal_schemaFunction · 0.85

Tested by

no test coverage detected