Class AlloyDBIfTool (0.17.0)

AlloyDBIfTool(
    *,
    name: str = "alloydb_if_tool",
    description: str = "A tool that uses AlloyDB AI to evaluate a semantic condition and returns True or False. Useful for semantic routing, classification, or filtering.",
    args_schema: typing.Type[
        pydantic.main.BaseModel
    ] = langchain_google_alloydb_pg.tools.AlloyDBIfInput,
    return_direct: bool = False,
    verbose: bool = False,
    callbacks: (
        list[langchain_core.callbacks.base.BaseCallbackHandler]
        | langchain_core.callbacks.base.BaseCallbackManager
        | None
    ) = None,
    tags: list[str] | None = None,
    metadata: dict[str, typing.Any] | None = None,
    handle_tool_error: (
        bool
        | str
        | collections.abc.Callable[
            [langchain_core.tools.base.ToolException],
            str | collections.abc.Sequence[str | dict[str, typing.Any]],
        ]
        | None
    ) = False,
    handle_validation_error: (
        bool
        | str
        | collections.abc.Callable[
            [
                pydantic_core._pydantic_core.ValidationError
                | pydantic.v1.error_wrappers.ValidationError
            ],
            str,
        ]
        | None
    ) = False,
    response_format: typing.Literal["content", "content_and_artifact"] = "content",
    extras: dict[str, typing.Any] | None = None,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None
)

Evaluates a natural-language condition with google_ml.if.

Returns True or False. Requires AlloyDB with google_ml_integration. Like any LangChain tool, call it with invoke / ainvoke or give it to an agent:

tool = AlloyDBIfTool(engine=engine)
tool.invoke({"condition": "Is 2 + 2 equal to 4?"})  # True

model_id works as in AlloyDBSentimentTool. If the model's reply is neither true nor false, google_ml.if returns NULL and the tool raises AlloyDBToolError rather than returning a value.

Methods

AlloyDBIfTool

AlloyDBIfTool(
    *,
    name: str = "alloydb_if_tool",
    description: str = "A tool that uses AlloyDB AI to evaluate a semantic condition and returns True or False. Useful for semantic routing, classification, or filtering.",
    args_schema: typing.Type[
        pydantic.main.BaseModel
    ] = langchain_google_alloydb_pg.tools.AlloyDBIfInput,
    return_direct: bool = False,
    verbose: bool = False,
    callbacks: (
        list[langchain_core.callbacks.base.BaseCallbackHandler]
        | langchain_core.callbacks.base.BaseCallbackManager
        | None
    ) = None,
    tags: list[str] | None = None,
    metadata: dict[str, typing.Any] | None = None,
    handle_tool_error: (
        bool
        | str
        | collections.abc.Callable[
            [langchain_core.tools.base.ToolException],
            str | collections.abc.Sequence[str | dict[str, typing.Any]],
        ]
        | None
    ) = False,
    handle_validation_error: (
        bool
        | str
        | collections.abc.Callable[
            [
                pydantic_core._pydantic_core.ValidationError
                | pydantic.v1.error_wrappers.ValidationError
            ],
            str,
        ]
        | None
    ) = False,
    response_format: typing.Literal["content", "content_and_artifact"] = "content",
    extras: dict[str, typing.Any] | None = None,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: typing.Optional[str] = None
)

Initialize the tool.

Exceptions
Type Description
TypeError If args_schema is not a subclass of pydantic BaseModel or dict.

_arun

_arun(
    condition: str,
    run_manager: typing.Optional[
        langchain_core.callbacks.manager.AsyncCallbackManagerForToolRun
    ] = None,
) -> bool

Evaluate the condition asynchronously. Called by ainvoke and arun.

_run

_run(
    condition: str,
    run_manager: typing.Optional[
        langchain_core.callbacks.manager.CallbackManagerForToolRun
    ] = None,
) -> bool

Evaluate the condition synchronously. Called by invoke and run.

model_post_init

model_post_init(context: Any, /) -> None

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.