airflow.providers.common.ai.toolsets.langchain_bridge

Bridge pydantic-ai toolsets into LangChain tools.

This is the reverse of pydantic-ai’s upstream pydantic_ai.ext.langchain bridge. Upstream turns LangChain tools into a pydantic-ai toolset (LangChainToolset) so they can be used with common.ai’s AgentOperator. This module goes the other way: it turns a pydantic-ai AbstractToolset – such as common.ai’s SQLToolset, HookToolset, or MCPToolset – into a list of LangChain StructuredTool objects, so Airflow’s curated tools can be handed to a LangChain agent or chain.

Functions

airflow_toolset_to_langchain_tools(toolset, *[, deps])

Convert a pydantic-ai toolset into a list of LangChain StructuredTool objects.

Module Contents

airflow.providers.common.ai.toolsets.langchain_bridge.airflow_toolset_to_langchain_tools(toolset, *, deps=None)[source]

Convert a pydantic-ai toolset into a list of LangChain StructuredTool objects.

Note

Experimental: this can change or be removed in a minor release of this provider. See Stable and experimental features.

Each returned tool carries the args_schema of the toolset’s tool, so a LangChain agent or chain can call it the same way it calls any native LangChain tool. What it returns passes through Airflow’s secret masker first.

A failure the model can correct reaches it as an error result, a LangChain ToolMessage with status="error", so it can try again: an argument that fails the toolset’s validation, or a pydantic-ai ModelRetry, which the bundled SQL toolsets raise to ask for a corrected query. These retries are bounded by the tool’s max_retries: once they are used up, and for any other exception the tool raises, the call raises ToolCallError, so the run fails instead of looping. A ValidationError raised by the tool itself also propagates, since the call may already have had a side effect.

The toolset’s get_tools is invoked eagerly here to enumerate the tools.

Warning

The bridge does not keep a toolset open between calls, so an MCPToolset reconnects to its server on every call, on the sync and async paths alike, and a stdio server loses any state it keeps between calls. A SandboxToolset has to be used inside its with block.

Note

A pydantic-ai toolset is normally driven inside an agent run, where a live RunContext carries the model, usage, and message history. Outside an agent run there is no such context, so this bridge builds a minimal one with an inert placeholder model. The curated common.ai toolsets (SQLToolset, HookToolset, MCPToolset) read only its retry budget, which the bridge sets, so this works for them. A custom toolset that reads live run state (ctx.model, ctx.messages, ctx.usage) will not behave correctly when bridged standalone.

Parameters:
  • toolset (pydantic_ai.toolsets.abstract.AbstractToolset[Any]) – The pydantic-ai toolset to convert.

  • deps (Any) – Optional dependency object exposed to the toolset as ctx.deps. Defaults to None.

Returns:

A list of LangChain StructuredTool objects, one per tool in the toolset.

Return type:

list[langchain_core.tools.StructuredTool]

Was this entry helpful?