airflow.providers.common.ai.hooks.langchain¶
Hook for LangChain integration with Airflow connections.
Classes¶
Bridge an Airflow connection to LangChain chat and embedding models. |
Module Contents¶
- class airflow.providers.common.ai.hooks.langchain.LangChainHook(llm_conn_id=None, embed_conn_id=None, llm_model=None, embed_model=None, *, embedding_kwargs=None, **kwargs)[source]¶
Bases:
airflow.providers.common.compat.sdk.BaseHookBridge an Airflow connection to LangChain chat and embedding models.
Note
Experimental: this can change or be removed in a minor release of this provider. See Stable and experimental features.
The hook resolves credentials (API key, optional base URL) from the Airflow connection and returns LangChain model objects via two universal entry-point functions:
langchain.chat_models.init_chat_model()dispatches to the right chat-model vendor based on the model identifier.langchain.embeddings.init_embeddings()dispatches to the right embedding-model vendor based on the model identifier.
Both identifiers use the
provider:nameformat (e.g."openai:gpt-5","openai:text-embedding-3-small"). The hook forwards the connection’s password asapi_keyand its host asbase_url, so any provider whose LangChain class accepts those two keyword arguments works (OpenAI, Anthropic, Groq, Mistral AI, Ollama, DeepSeek, …). Providers with bespoke auth (AWS Bedrock, Google Vertex AI / GenAI, Azure OpenAI, Cohere, HuggingFace) reject these kwargs.Connection fields:
password: API key passed as
api_key=to the model constructor.host: Optional base URL passed as
base_url=(custom endpoints, Ollama, vLLM).extra JSON:
{"model": "openai:gpt-5", "embed_model": "openai:text-embedding-3-small"}– default chat and embedding model identifiers.
- Parameters:
llm_conn_id (str | None) – Airflow connection ID for the LLM provider. Falls back to
default_conn_name("langchain_default") if not provided.embed_conn_id (str | None) – Optional separate Airflow connection ID for the embedding provider. Falls back to
llm_conn_idwhen not provided – the common case of one provider for both chat and embeddings stays a single hook instance.llm_model (str | None) – Chat model identifier in
provider:nameformat (e.g."openai:gpt-5","anthropic:claude-sonnet-5"). Overridesextra["model"]on the connection.embed_model (str | None) – Embedding model identifier in
provider:nameformat (e.g."openai:text-embedding-3-small"). Overridesextra["embed_model"]on the connection. Whenembedding_kwargssuppliesproviderexplicitly, use a model name without the provider prefix.embedding_kwargs (dict[str, Any] | None) – Additional keyword arguments to pass to the embedding model constructor without filtering. Values can override hook-provided settings, including the endpoint and credentials. In particular,
providertakes precedence over the provider inferred fromembed_model. Whenprovideris set, LangChain treats the entireembed_modelvalue as the model name rather than parsing aprovider:nameidentifier. The hook logs a warning when both forms are supplied. Connectionapi_keyandbase_urlvalues take precedence over the same top-level keys, but the underlying integration may accept alternative or nested options that take precedence. Only pass trusted values.
- static get_ui_field_behaviour()[source]¶
Return custom field behaviour for the Airflow connection form.
- get_chat_model()[source]¶
Return a LangChain chat model configured from the Airflow connection.
Dispatch is delegated to
init_chat_model, which picks the right vendor class based on theprovider:nameprefix in the model id.
- get_embedding_model()[source]¶
Return a LangChain embedding model configured from the Airflow connection.
Dispatch is delegated to
init_embeddings, which picks the right vendor class based on theprovider:nameprefix in the model id. Usesembed_conn_idif set (falls back tollm_conn_id).
- test_connection()[source]¶
Test connection by resolving the chat model.
Validates that the model identifier is valid and the provider can be instantiated with the supplied credentials. Does NOT make an LLM API call – that would be expensive and fail for reasons unrelated to connectivity (quotas, billing, rate limits).