{ "cells": [ { "cell_type": "markdown", "id": "e327e9bd-effc-4bee-a875-1c383c17f43d", "metadata": {}, "source": [ "# Complex data extraction with function calling\n", "\n", "Function calling is a core primitive for integrating LLMs within your software stack. We use it throughout the LangGraph docs, since developing with function calling (aka tool usage) tends to be much more stress-free than the traditional way of writing custom string parsers.\n", "\n", "However, even GPT-4, Opus, and other powerful models still struggle with complex functions, especially if your schema involves any nesting or if you have more advanced data validation rules.\n", "\n", "There are three basic ways to increase reliability: better prompting, constrained decoding, and **validation with re-prompting**.\n", "\n", "We will cover two approaches to the last technique here, since it is generally applicable across any LLM that supports tool calling.\n", "\n", "## Setup\n", "\n", "First, let's install the required packages and set our API keys" ] }, { "cell_type": "code", "execution_count": 1, "id": "0ada5e8f-3f2f-459e-83aa-6cd8861770dd", "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U langchain-anthropic langgraph" ] }, { "cell_type": "code", "execution_count": 2, "id": "c0acb818-b6fd-48ab-97e6-fc2de2d03e87", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "\n", "def _set_env(var: str):\n", " if not os.environ.get(var):\n", " os.environ[var] = getpass.getpass(f\"{var}: \")\n", "\n", "\n", "_set_env(\"OPENAI_API_KEY\")" ] }, { "cell_type": "markdown", "id": "f07bc7a6", "metadata": {}, "source": [ "
Set up LangSmith for LangGraph development
\n", "\n", " Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here. \n", "
\n", "Using Pydantic with LangChain
\n", "\n",
" This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
"