Given a Python function, extracts a `FuncSchema` from it, capturing the name, description, parameter descriptions, and other metadata. Args: func: The function to extract the schema from. docstring_style: The style of the docstring to use for parsing. If not provided, w
(
func: Callable[..., Any],
docstring_style: DocstringStyle | None = None,
name_override: str | None = None,
description_override: str | None = None,
use_docstring_info: bool = True,
strict_json_schema: bool = True,
)
| 222 | |
| 223 | |
| 224 | def function_schema( |
| 225 | func: Callable[..., Any], |
| 226 | docstring_style: DocstringStyle | None = None, |
| 227 | name_override: str | None = None, |
| 228 | description_override: str | None = None, |
| 229 | use_docstring_info: bool = True, |
| 230 | strict_json_schema: bool = True, |
| 231 | ) -> FuncSchema: |
| 232 | """ |
| 233 | Given a Python function, extracts a `FuncSchema` from it, capturing the name, description, |
| 234 | parameter descriptions, and other metadata. |
| 235 | |
| 236 | Args: |
| 237 | func: The function to extract the schema from. |
| 238 | docstring_style: The style of the docstring to use for parsing. If not provided, we will |
| 239 | attempt to auto-detect the style. |
| 240 | name_override: If provided, use this name instead of the function's `__name__`. |
| 241 | description_override: If provided, use this description instead of the one derived from the |
| 242 | docstring. |
| 243 | use_docstring_info: If True, uses the docstring to generate the description and parameter |
| 244 | descriptions. |
| 245 | strict_json_schema: Whether the JSON schema is in strict mode. If True, we'll ensure that |
| 246 | the schema adheres to the "strict" standard the OpenAI API expects. We **strongly** |
| 247 | recommend setting this to True, as it increases the likelihood of the LLM producing |
| 248 | correct JSON input. |
| 249 | |
| 250 | Returns: |
| 251 | A `FuncSchema` object containing the function's name, description, parameter descriptions, |
| 252 | and other metadata. |
| 253 | """ |
| 254 | |
| 255 | # 1. Grab docstring info |
| 256 | if use_docstring_info: |
| 257 | doc_info = generate_func_documentation(func, docstring_style) |
| 258 | param_descs = dict(doc_info.param_descriptions or {}) |
| 259 | else: |
| 260 | doc_info = None |
| 261 | param_descs = {} |
| 262 | |
| 263 | type_hints_with_extras = get_type_hints(func, include_extras=True) |
| 264 | type_hints: dict[str, Any] = {} |
| 265 | annotated_param_descs: dict[str, str] = {} |
| 266 | param_metadata: dict[str, tuple[Any, ...]] = {} |
| 267 | |
| 268 | for name, annotation in type_hints_with_extras.items(): |
| 269 | if name == "return": |
| 270 | continue |
| 271 | |
| 272 | stripped_ann, metadata = _strip_annotated(annotation) |
| 273 | type_hints[name] = stripped_ann |
| 274 | param_metadata[name] = metadata |
| 275 | |
| 276 | description = _extract_description_from_metadata(metadata) |
| 277 | if description is not None: |
| 278 | annotated_param_descs[name] = description |
| 279 | |
| 280 | for name, description in annotated_param_descs.items(): |
| 281 | param_descs.setdefault(name, description) |