{"id":4312,"library":"unitycatalog-langchain","title":"Unity Catalog LangChain Integration","description":"The `unitycatalog-langchain` library provides seamless integration of Unity Catalog (UC) functions as tools within LangChain agent applications. It enables developers to define and manage AI functions in Unity Catalog and utilize them across various GenAI platforms, including LangChain, LlamaIndex, OpenAI, and Anthropic. The current version is 0.3.0 and it is actively maintained with a focus on interoperability and secure access control for AI assets.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/unitycatalog/unitycatalog","tags":["LangChain","Unity Catalog","AI functions","tooling","Databricks","LLM"],"install":[{"cmd":"pip install unitycatalog-langchain","lang":"bash","label":"Base Installation"},{"cmd":"pip install unitycatalog-langchain[databricks]","lang":"bash","label":"Databricks Integration (optional)"}],"dependencies":[{"reason":"Core client library for interacting with Unity Catalog AI functions.","package":"unitycatalog-ai","optional":false},{"reason":"Framework for building LLM applications, which this library integrates with.","package":"langchain","optional":false},{"reason":"Required for integration with Databricks-managed Unity Catalog and its specific LangChain components.","package":"databricks-langchain","optional":true}],"imports":[{"symbol":"UCFunctionToolkit","correct":"from unitycatalog.langchain import UCFunctionToolkit"},{"symbol":"ApiClient","correct":"from unitycatalog.client import ApiClient"},{"symbol":"Configuration","correct":"from unitycatalog.client import Configuration"},{"symbol":"UnitycatalogFunctionClient","correct":"from unitycatalog.ai.core.client import UnitycatalogFunctionClient"},{"note":"Use this for Databricks-managed Unity Catalog.","symbol":"DatabricksFunctionClient","correct":"from unitycatalog.ai.core.databricks import DatabricksFunctionClient"},{"note":"A simplified way to initialize the UC function client, especially in Databricks environments.","symbol":"get_uc_function_client","correct":"from unitycatalog.ai.core.base import get_uc_function_client"}],"quickstart":{"code":"import os\nfrom unitycatalog.client import ApiClient, Configuration\nfrom unitycatalog.ai.core.client import UnitycatalogFunctionClient\nfrom unitycatalog.langchain import UCFunctionToolkit\nfrom langchain.agents import AgentExecutor, create_tool_calling_agent\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_openai import ChatOpenAI # Using OpenAI for demonstration\n\n# --- Configuration for Unity Catalog Client ---\n# Replace with your actual Unity Catalog host and token\nUC_HOST = os.environ.get('UC_HOST', 'http://localhost:8080/api/2.1/unity-catalog')\n# UC_TOKEN is often not directly used in 'http://localhost' setups, but for remote UC, it's essential.\n# You might use Databricks personal access token or similar. For local testing, usually not needed.\n# UC_TOKEN = os.environ.get('UC_TOKEN', 'YOUR_UC_AUTH_TOKEN') \n\nconfig = Configuration(host=UC_HOST)\n# If a token is required, you might set it like: config.access_token = UC_TOKEN\napi_client = ApiClient(configuration=config)\nuc_client = UnitycatalogFunctionClient(api_client=api_client)\n\n# --- Define a simple Python function to be registered with Unity Catalog ---\ndef add_numbers(number_1: float, number_2: float) -> float:\n    \"\"\"A function that accepts two floating point numbers, adds them, and returns the resulting sum as a float.\n\n    Args:\n        number_1 (float): The first of the two numbers to add.\n        number_2 (float): The second of the two numbers to add.\n\n    Returns:\n        float: The sum of the two input numbers.\n    \"\"\"\n    return number_1 + number_2\n\n# --- Register the function with Unity Catalog (example placeholders) ---\nCATALOG = os.environ.get('UC_CATALOG', 'my_catalog')\nSCHEMA = os.environ.get('UC_SCHEMA', 'my_schema')\n\n# In a real scenario, ensure CATALOG and SCHEMA exist in your UC instance.\n# The `create_python_function` would typically be called once to register.\n# For a runnable quickstart, we'll mock its presence or assume it's pre-registered.\n# If running against a real UC, you'd uncomment and run this once:\n# function_info = uc_client.create_python_function(\n#     func=add_numbers,\n#     catalog=CATALOG,\n#     schema=SCHEMA,\n#     replace=True\n# )\n# print(f\"Registered function: {function_info.name}\")\n\n# --- Retrieve the function and create a LangChain toolkit ---\n# Assuming 'add_numbers' is already registered in 'my_catalog.my_schema'\nfunction_reference = f\"{CATALOG}.{SCHEMA}.add_numbers\"\ntoolkit = UCFunctionToolkit(client=uc_client, function_names=[function_reference])\n\n# --- Use the tool in a LangChain Agent ---\nllm = ChatOpenAI(openai_api_key=os.environ.get('OPENAI_API_KEY'))\n\nprompt = ChatPromptTemplate.from_messages([\n    (\"system\", \"You are a helpful assistant. Make sure to use tools for additional functionality.\"),\n    (\"placeholder\", \"{chat_history}\"),\n    (\"human\", \"{input}\"),\n    (\"placeholder\", \"{agent_scratchpad}\"),\n])\n\nagent = create_tool_calling_agent(llm, toolkit.get_tools(), prompt)\nagent_executor = AgentExecutor(agent=agent, tools=toolkit.get_tools(), verbose=True)\n\nresponse = agent_executor.invoke({\"input\": \"What is 36939.0 + 8922.4?\"})\nprint(response)\n","lang":"python","description":"This quickstart demonstrates how to set up the `unitycatalog-langchain` client, define a Python function, register it (conceptually, as a live UC server is needed for actual registration), retrieve it via `UCFunctionToolkit`, and integrate it into a LangChain agent. It assumes an existing Unity Catalog server and requires `OPENAI_API_KEY` for the LLM example. Replace placeholder values like `UC_HOST`, `UC_CATALOG`, `UC_SCHEMA` with your actual Unity Catalog configuration."},"warnings":[{"fix":"Migrate LangChain agent and prompt creation to the new API (e.g., `create_tool_calling_agent`, `ChatPromptTemplate.from_messages`). Refer to the official LangChain v1.0 upgrade guide for details. `unitycatalog-langchain` itself supports LangChain v1.x.","message":"LangChain 1.0 introduced significant breaking changes in its API, particularly for agents and prompt engineering. If upgrading from `langchain` v0.x, code using `initialize_agent` or older prompt formats will break.","severity":"breaking","affected_versions":"LangChain < 1.0 to LangChain >= 1.0"},{"fix":"Ensure all function arguments and return types are explicitly hinted. Write comprehensive Google-style docstrings explaining the function's purpose, arguments, and return value.","message":"When defining Python functions for Unity Catalog, all arguments and the return value must be properly typed, and the docstring should follow Google-style guidelines including descriptions for the function, arguments, and return. Failure to do so can lead to incorrect interpretation by the LLM.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify `UC_HOST` and authentication mechanism (e.g., token) based on your Unity Catalog deployment. For Databricks, consider using `from unitycatalog.ai.core.base import get_uc_function_client` for simpler client initialization.","message":"For local Unity Catalog server setups, ensure the `UC_HOST` is correctly configured (e.g., `http://localhost:8080/api/2.1/unity-catalog`). For Databricks Unity Catalog, the client initialization may differ, often using `DatabricksFunctionClient` or `get_uc_function_client` without manually specifying host/token if running within a Databricks environment.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}