Home / Function/ create_schema_from_function() — langchain Function Reference

create_schema_from_function() — langchain Function Reference

Architecture documentation for the create_schema_from_function() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ce65f72d_5a31_1cf5_28d0_0114514b29b5["create_schema_from_function()"]
  80cebff4_efbd_4311_85e9_de0dc81a7eee["base.py"]
  ce65f72d_5a31_1cf5_28d0_0114514b29b5 -->|defined in| 80cebff4_efbd_4311_85e9_de0dc81a7eee
  ecf72290_45d2_5efc_567a_33bf808fcd4e["get_input_schema()"]
  ecf72290_45d2_5efc_567a_33bf808fcd4e -->|calls| ce65f72d_5a31_1cf5_28d0_0114514b29b5
  198ed473_bade_ca88_bf38_3399498f9947["_function_annotations_are_pydantic_v1()"]
  ce65f72d_5a31_1cf5_28d0_0114514b29b5 -->|calls| 198ed473_bade_ca88_bf38_3399498f9947
  5c55c769_8605_2bfb_879c_38c645ebb4f8["_is_injected_arg_type()"]
  ce65f72d_5a31_1cf5_28d0_0114514b29b5 -->|calls| 5c55c769_8605_2bfb_879c_38c645ebb4f8
  1a23ba05_08c6_e278_462c_5ad74643f2c9["_infer_arg_descriptions()"]
  ce65f72d_5a31_1cf5_28d0_0114514b29b5 -->|calls| 1a23ba05_08c6_e278_462c_5ad74643f2c9
  style ce65f72d_5a31_1cf5_28d0_0114514b29b5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/tools/base.py lines 289–387

def create_schema_from_function(
    model_name: str,
    func: Callable,
    *,
    filter_args: Sequence[str] | None = None,
    parse_docstring: bool = False,
    error_on_invalid_docstring: bool = False,
    include_injected: bool = True,
) -> type[BaseModel]:
    """Create a Pydantic schema from a function's signature.

    Args:
        model_name: Name to assign to the generated Pydantic schema.
        func: Function to generate the schema from.
        filter_args: Optional list of arguments to exclude from the schema.

            Defaults to `FILTERED_ARGS`.
        parse_docstring: Whether to parse the function's docstring for descriptions
            for each argument.
        error_on_invalid_docstring: If `parse_docstring` is provided, configure
            whether to raise `ValueError` on invalid Google Style docstrings.
        include_injected: Whether to include injected arguments in the schema.

            Defaults to `True`, since we want to include them in the schema when
            *validating* tool inputs.

    Returns:
        A Pydantic model with the same arguments as the function.
    """
    sig = inspect.signature(func)

    if _function_annotations_are_pydantic_v1(sig, func):
        validated = validate_arguments_v1(func, config=_SchemaConfig)  # type: ignore[call-overload]
    else:
        # https://docs.pydantic.dev/latest/usage/validation_decorator/
        with warnings.catch_warnings():
            # We are using deprecated functionality here.
            # This code should be re-written to simply construct a Pydantic model
            # using inspect.signature and create_model.
            warnings.simplefilter("ignore", category=PydanticDeprecationWarning)
            validated = validate_arguments(func, config=_SchemaConfig)  # type: ignore[operator]

    # Let's ignore `self` and `cls` arguments for class and instance methods
    # If qualified name has a ".", then it likely belongs in a class namespace
    in_class = bool(func.__qualname__ and "." in func.__qualname__)

    has_args = False
    has_kwargs = False

    for param in sig.parameters.values():
        if param.kind == param.VAR_POSITIONAL:
            has_args = True
        elif param.kind == param.VAR_KEYWORD:
            has_kwargs = True

    inferred_model = validated.model

    if filter_args:
        filter_args_ = filter_args
    else:
        # Handle classmethods and instance methods
        existing_params: list[str] = list(sig.parameters.keys())
        if existing_params and existing_params[0] in {"self", "cls"} and in_class:
            filter_args_ = [existing_params[0], *list(FILTERED_ARGS)]
        else:
            filter_args_ = list(FILTERED_ARGS)

        for existing_param in existing_params:
            if not include_injected and _is_injected_arg_type(
                sig.parameters[existing_param].annotation
            ):
                filter_args_.append(existing_param)

    description, arg_descriptions = _infer_arg_descriptions(
        func,
        parse_docstring=parse_docstring,
        error_on_invalid_docstring=error_on_invalid_docstring,
    )
    # Pydantic adds placeholder virtual fields we need to strip
    valid_properties = []
    for field in get_fields(inferred_model):

Subdomains

Called By

Frequently Asked Questions

What does create_schema_from_function() do?
create_schema_from_function() is a function in the langchain codebase, defined in libs/core/langchain_core/tools/base.py.
Where is create_schema_from_function() defined?
create_schema_from_function() is defined in libs/core/langchain_core/tools/base.py at line 289.
What does create_schema_from_function() call?
create_schema_from_function() calls 3 function(s): _function_annotations_are_pydantic_v1, _infer_arg_descriptions, _is_injected_arg_type.
What calls create_schema_from_function()?
create_schema_from_function() is called by 1 function(s): get_input_schema.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free