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¶
|
Convert a pydantic-ai toolset into a list of LangChain |
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
StructuredToolobjects.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_schemaof 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
ToolMessagewithstatus="error", so it can try again: an argument that fails the toolset’s validation, or a pydantic-aiModelRetry, which the bundled SQL toolsets raise to ask for a corrected query. These retries are bounded by the tool’smax_retries: once they are used up, and for any other exception the tool raises, the call raisesToolCallError, so the run fails instead of looping. AValidationErrorraised by the tool itself also propagates, since the call may already have had a side effect.The toolset’s
get_toolsis invoked eagerly here to enumerate the tools.Warning
The bridge does not keep a toolset open between calls, so an
MCPToolsetreconnects to its server on every call, on the sync and async paths alike, and a stdio server loses any state it keeps between calls. ASandboxToolsethas to be used inside itswithblock.Note
A pydantic-ai toolset is normally driven inside an agent run, where a live
RunContextcarries 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 toNone.
- Returns:
A list of LangChain
StructuredToolobjects, one per tool in the toolset.- Return type:
list[langchain_core.tools.StructuredTool]