MCPcopy Create free account
hub / github.com/SqueezeAILab/LLMCompiler / create_schema_from_function

Function create_schema_from_function

src/tools/base.py:58–80  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

56
57
58def 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
83class ToolException(Exception):

Callers 1

from_functionMethod · 0.85

Calls 2

_get_filtered_argsFunction · 0.85
_create_subset_modelFunction · 0.85

Tested by

no test coverage detected