Create a pydantic schema from a function's signature. Args: model_name: Name to assign to the generated pydandic schema func: Function to generate the schema from Returns: A pydantic model with the same arguments as the function
(
model_name: str,
func: Callable,
)
| 56 | |
| 57 | |
| 58 | def create_schema_from_function( |
| 59 | model_name: str, |
| 60 | func: Callable, |
| 61 | ) -> Type[BaseModel]: |
| 62 | """Create a pydantic schema from a function's signature. |
| 63 | Args: |
| 64 | model_name: Name to assign to the generated pydandic schema |
| 65 | func: Function to generate the schema from |
| 66 | Returns: |
| 67 | A pydantic model with the same arguments as the function |
| 68 | """ |
| 69 | # https://docs.pydantic.dev/latest/usage/validation_decorator/ |
| 70 | validated = validate_arguments(func, config=_SchemaConfig) # type: ignore |
| 71 | inferred_model = validated.model # type: ignore |
| 72 | if "run_manager" in inferred_model.__fields__: |
| 73 | del inferred_model.__fields__["run_manager"] |
| 74 | if "callbacks" in inferred_model.__fields__: |
| 75 | del inferred_model.__fields__["callbacks"] |
| 76 | # Pydantic adds placeholder virtual fields we need to strip |
| 77 | valid_properties = _get_filtered_args(inferred_model, func) |
| 78 | return _create_subset_model( |
| 79 | f"{model_name}Schema", inferred_model, list(valid_properties) |
| 80 | ) |
| 81 | |
| 82 | |
| 83 | class ToolException(Exception): |
no test coverage detected