{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Juan Carlos Olamendy",
  "title_listed": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://juancolamendy.hashnode.dev/parsing-llm-structured-outputs-in-langchain-a-comprehensive-guide",
  "publish_date": "2024-10-22",
  "corpus_stage": "",
  "notes": "",
  "id": "15003105266b",
  "collected_at": "2026-10-02T14:10:52",
  "title": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
  "body_markdown": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide\nPublished\nOctober 22, 2024\n•\n11\nmin read\n•\nView as Markdown\nJ\nJuan Carlos Olamendy\nSenior AI Engineer | Building & Telling Stories about AI/ML Systems | Software Engineer\nParsing structured outputs from Large Language Models (LLMs) is a crucial skill when developing AI-powered applications.\nIt allows developers to extract precise, predictable information from models, transforming unstructured text into structured data.\nIn LangChain, there are various methods for parsing structured outputs from LLMs, each with its own strengths and use cases.\nIn this article, we'll explore three primary techniques: the with_structured_output method, the PydanticOutputParser class, and the StructuredOutputParser class.\nRead the whole post to discover how to use these methods provide the flexibility to handle different requirements, ensuring type safety, and enable users to extract key-value information accurately from LLM outputs.\nThe Importance of Structured Outputs\nWorking with structured outputs is one of the key components of many AI-driven applications.\nBy extracting structured data from LLMs, we can integrate the responses more seamlessly into our software solutions.\nLangChain provides methods to do this with varying levels of type safety and flexibility, allowing developers to define specific formats for model responses.\nwith_structured_output\nMethod\nThe\nwith_structured_output\nmethod is a powerful feature in LangChain that allows you to guide LLMs in generating structured responses based on a provided schema.\nIt's like giving the LLM a blueprint for its output, ensuring that the response adheres to a specific format.\nThis method shines when you're working with LLMs that support JSON mode or structured output APIs, such as OpenAI and Anthropic.\nHowever, if the model doesn't natively support such features, you need to use an output parser, such as the PydanticOutputParser or StructuredOutputParser, to extract the structured response.\nIt's particularly useful when you need to:\nEnforce a specific output structure directly within the prompt.\nLeverage the LLM's native capabilities for structured outputs.\nEnsure type safety and validation of the generated content.\nWeather Forecast Example\nImagine we want to create a weather forecast generator using an LLM.\nWe'll use the\nwith_structured_output\nmethod to ensure our weather forecast is properly formatted.\nfrom\nlangchain_core.pydantic_v1\nimport\nBaseModel, Field\nfrom\nlangchain_openai\nimport\nChatOpenAI\nfrom\nlangchain_core.prompts\nimport\nChatPromptTemplate\n# Define our WeatherForecast model\nclass\nWeatherForecast\n(\nBaseModel\n):\ntemperature: float = Field(description=\n\"The temperature in Celsius\"\n)\n    condition: str = Field(description=\n\"The weather condition (e.g., sunny, rainy, cloudy)\"\n)\n    humidity: int = Field(description=\n\"The humidity percentage\"\n)\n    wind_speed: float = Field(description=\n\"The wind speed in km/h\"\n)\n# Set up the LLM\nmodel = ChatOpenAI(model=\n\"gpt-4\"\n, temperature=\n0\n)\n# Create the prompt template\nprompt = ChatPromptTemplate.from_template(\n\"Given the context below, provide a weather forecast for {city} tomorrow, respond in JSON with `temperature`, `condition`, `humidity`, and `wind_speed` keys\\n\\n{context}\"\n)\n# Apply structured output to the LLM\nstructured_llm = model.with_structured_output(WeatherForecast, method=\n\"json_mode\"\n)\n# Chain the prompt and structured LLM\nweather_chain = prompt | structured_llm\n# Generate a weather forecast\nresult = weather_chain.invoke({\n\"city\"\n:\n\"New York\"\n,\n\"context\"\n:\n\"The weather in New York will be sunny with a chance of rain.\"\n})\n\nprint(result)\n# Output: WeatherForecast(temperature=22.5, condition='Partly cloudy', humidity=65, wind_speed=10.2)\nIn this example, we've defined a\nWeatherForecast\nmodel using Pydantic, specifying the structure we want for our weather forecast. The prompt template now asks for a weather forecast for a given city. And finally, we use with_structured_output to ensure the LLM's response adheres to the WeatherForecast structure.\nBenefits of\nwith_structured_output\nType safety: The Pydantic model ensures that the output matches the expected types.\nIntegration with LangChain's runnables: This method wraps the LLM call in a runnable, allowing for easy chaining of operations.\nFlexibility: You can use various schema types, including TypeDict, JSON schema, or Pydantic classes.\nLimitations and Considerations\nWhile powerful,\nwith_structured_output\nhas some limitations to keep in mind:\nLLM compatibility: It only works with LLMs that support structured output APIs.\nComplexity in advanced scenarios: When dealing with more complex workflows, the use of runnables can introduce additional complexity.\nLearning curve: Understanding LangChain's expression language (LCEL) and runnables requires some additional learning.\nThe PydanticOutputParser Class\nThe\nPydanticOutputParser\nclass is another tool in LangChain's arsenal for extracting structured information from LLM outputs.\nIt's particularly useful when working with LLMs that don't support native structured output features.\nIt allows you to enforce type safety by ensuring that the output conforms to a predefined Pydantic schema.\nThis parser works best when you need reliable type validation or when your LLM lacks structured output features.\nFilm Festival Example\nSuppose we want to create a system that extracts structured information about movies from an LLM's output.\nfrom\ntyping\nimport\nList\nfrom\nlangchain_core.output_parsers\nimport\nPydanticOutputParser\nfrom\nlangchain_core.prompts\nimport\nChatPromptTemplate\nfrom\nlangchain_core.pydantic_v1\nimport\nBaseModel, Field\nfrom\nlangchain_openai\nimport\nChatOpenAI\n# Define our Movie and FilmFestival models\nclass\nMovie\n(\nBaseModel\n):\ntitle: str = Field(..., description=\n\"The title of the movie\"\n)\n    director: str = Field(..., description=\n\"The director of the movie\"\n)\n    runtime: int = Field(..., description=\n\"The runtime of the movie in minutes\"\n)\nclass\nFilmFestival\n(\nBaseModel\n):\nmovies: List[Movie]\n# Set up the parser\nparser = PydanticOutputParser(pydantic_object=FilmFestival)\n# Create the prompt template\nprompt = ChatPromptTemplate.from_messages([\n    (\n\"system\"\n,\n\"Answer the user query about movies in the film festival. Wrap the output in `json` format following the schema below\\n{format_instructions}\"\n),\n    (\n\"human\"\n,\n\"{query}\"\n),\n]).partial(format_instructions=parser.get_format_instructions())\n# Set up the LLM and chain\nllm = ChatOpenAI(model=\n\"gpt-4\"\n, temperature=\n0\n)\nchain = prompt | llm | parser\n# Generate movie information\nquery =\n\"Please provide details about the movies 'Inception' directed by Christopher Nolan with a runtime of 148 minutes and 'Parasite' directed by Bong Joon-ho with a runtime of 132 minutes.\"\nresult = chain.invoke({\n\"query\"\n: query})\n\nprint(result)\n# Expected output: FilmFestival(movies=[Movie(title='Inception', director='Christopher Nolan', runtime=148), Movie(title='Parasite', director='Bong Joon-ho', runtime=132)])\nIn this example, we've defined\nMovie\nand\nFilmFestival\nmodels using Pydantic.\nWe've defined\nMovie\nand\nFilmFestival\nmodels using Pydantic, specifying the structure we want for our movie information.\nThe\nMovie\nmodel includes fields for the title, director, and runtime of each movie.\nThe\nFilmFestival\nmodel contains a list of\nMovie\nobjects.\nThe prompt template now asks for details about movies in a film festival.\nWe use\nPydanticOutputParser\nto ensure the LLM's output adheres to our defined structure.\nThe chain is invoked with a query asking for information about two specific movies.\nBenefits of PydanticOutputParser\nStrict type enforcement: Ensures that the LLM output matches the expected data types.\nFlexible schema definition: Allows for complex nested structures and custom validation logic.\nIntegration with LangChain's runnable interface: Enables easy incorporation into LangChain workflows.\nLimitations and Considerations\nWhile powerful,\nPydanticOutputParser\nhas some limitations to keep in mind:\nOverhead: Requires additional processing to parse the LLM output.\nPrompt engineering: Needs careful prompt design to guide the LLM in producing the correct output format.\nLearning curve: Requires familiarity with Pydantic for defining schemas.\nThe StructuredOutputParser Class\nThe\nStructuredOutputParser\nclass is a versatile tool in LangChain that allows you to extract structured information from LLM outputs using custom-defined schemas.\nIt works with diverse models and provides flexibility to define custom output structures through ResponseSchema objects.\nThis parser is particularly useful when you need to extract information that doesn't fit neatly into pre-built schemas.\nExample: Recipe and Ingredient Extractor\nImagine we want to create a system that extracts recipes and ingredient lists from an LLM's output.\nfrom\nlangchain.output_parsers\nimport\nResponseSchema, StructuredOutputParser\nfrom\nlangchain_core.prompts\nimport\nPromptTemplate\nfrom\nlangchain_openai\nimport\nChatOpenAI\n# Define the response schemas\nresponse_schemas = [\n    ResponseSchema(name=\n\"recipe\"\n, description=\n\"the recipe for the dish requested by the user\"\n),\n    ResponseSchema(name=\n\"ingredients\"\n, description=\n\"list of ingredients required for the recipe, should be a detailed list\"\n),\n]\n# Create the parser\noutput_parser = StructuredOutputParser.from_response_schemas(response_schemas)\n# Generate format instructions\nformat_instructions = output_parser.get_format_instructions()\n# Create the prompt template\nprompt = PromptTemplate(\n    template=\n\"Provide the recipe for the dish requested.\\n{format_instructions}\\n{dish}\"\n,\n    input_variables=[\n\"dish\"\n],\n    partial_variables={\n\"format_instructions\"\n: format_instructions},\n)\n# Set up the LLM and chain\nmodel = ChatOpenAI(model=\n\"gpt-4-0613\"\n, temperature=\n0\n)\nchain = prompt | model | output_parser\n# Generate recipe and ingredients\nresponse = chain.invoke({\n\"dish\"\n:\n\"Spaghetti Bolognese\"\n})\n\nprint(response)\n# Output:\n# {\n#   \"recipe\": \"To make Spaghetti Bolognese, cook minced beef with onions, garlic, tomatoes, and Italian herbs. Simmer until thickened and serve over cooked spaghetti.\",\n#   \"ingredients\": \"Minced beef, onions, garlic, tomatoes, Italian herbs, spaghetti, olive oil, salt, pepper.\"\n# }\nIn this example, we've defined two\nResponseSchema\nobjects for \"recipe\" and \"ingredients\".\nThe\nStructuredOutputParser\nensures that the LLM's output contains these specific fields, allowing us to easily extract and use this information.\nBenefits of StructuredOutputParser\nFlexibility: Allows for custom-defined output structures tailored to your specific needs.\nWide compatibility: Works with a broad range of LLMs, including those without native structured output support.\nFine-grained control: Enables extraction of specific key-value pairs from the LLM's response.\nLimitations and Considerations\nWhile versatile,\nStructuredOutputParser\nhas some limitations to keep in mind:\nComplexity in schema definition: Requires careful definition of\nResponseSchema\nobjects.\nPrompt engineering: Needs thoughtful prompt design to guide the LLM in producing the correct output format.\nLess rigid type enforcement: Compared to\nPydanticOutputParser\n, it provides less strict type validation.\nChoosing the Right Parser for Your Needs\nNow that we've explored the three main methods for parsing structured outputs in LangChain, you might be wondering which one to choose for your specific use case. Here's a quick guide to help you decide:\nUse\nwith_structured_output\nwhen:\nYou're working with LLMs that support native structured output features (e.g., OpenAI, Anthropic, Cohere).\nYou want to define the output schema directly within the prompt.\nType safety and validation are essential.\nYou're comfortable working with LangChain's runnables.\nChoose\nPydanticOutputParser\nwhen:\nYou need strict type safety and validation.\nYour LLM might not support structured output features directly.\nYou're familiar with Pydantic models for schema definition.\nYou're working with complex, nested data structures.\nOpt for\nStructuredOutputParser\nwhen:\nYou require custom and flexible output structures.\nExisting parsers don't handle your desired schema.\nYou need fine-grained control over the extracted information.\nYou're working with a wide range of LLMs, including those without native structured output support.\nRemember, the best choice often depends on your specific use case, the LLMs you're working with, and your familiarity with different schema definition methods.\nBest Practices for Parsing Structured Outputs\nIf you like this article, share it with others ♻️\nWould help a lot ❤️\nAnd feel free to follow me for articles more like this.\nRegardless of which parser you choose, here are some best practices to keep in mind when working with structured outputs in LangChain:\nDesign clear schemas: Clearly define your output structure, considering all possible fields and their types.\nUse descriptive field names: Choose field names that are self-explanatory and consistent with your application's terminology.\nProvide detailed descriptions: When defining schemas, include clear descriptions for each field to guide the LLM.\nCraft effective prompts: Design your prompts carefully to guide the LLM in producing the desired output format.\nHandle errors gracefully: Implement error handling to deal with cases where the LLM's output doesn't match the expected structure.\nTest thoroughly: Test your parsing logic with a variety of inputs to ensure robustness.\nConsider performance: Be mindful of the performance impact of parsing, especially when dealing with large volumes of data.\nKeep it simple: Start with simple structures and gradually increase complexity as needed.\nDocument your schemas: Maintain clear documentation of your output schemas for ease of maintenance and collaboration.\nStay updated: Keep an eye on LangChain updates, as new features and improvements to parsing capabilities are regularly released.\nFAQs\nIf you like this article, share it with others ♻️\nWould help a lot ❤️\nAnd feel free to follow me for articles more like this.\nWhat are output parsers in LangChain?\nOutput parsers structure and extract specific information from unstructured LLM outputs, ensuring the responses adhere to a predefined schema.\nWhat's the difference between with_structured_output, PydanticOutputParser, and StructuredOutputParser?\nwith_structured_output works with LLMs that support structured output APIs, using schemas like Pydantic models to guide responses.\nPydanticOutputParser enforces type safety for structured outputs using Pydantic schemas.\nStructuredOutputParser provides flexibility for custom output structures with user-defined response schemas.\nWhen should I use with_structured_output?\nUse it when your LLM natively supports structured output features, and you want to define the output schema directly within the prompt.\nWhen is PydanticOutputParser a good choice?\nChoose it when strict type safety and validation are required, especially if the LLM doesn’t support structured output natively.\nIn what situations should I consider StructuredOutputParser?\nUse it when you require a custom output structure or need fine-grained control over the extracted information.\nCan I use these output parsers with any LLM?\nwith_structured_output is limited to compatible LLMs, while PydanticOutputParser and StructuredOutputParser work with a broader range of models.\nWhat are the benefits of using output parsers in LangChain?\nThey transform unstructured LLM output into organized data, enforce type safety, and provide flexibility in schema definition.\nConclusion\nParsing structured outputs from LLMs is a crucial skill in the world of AI-powered applications.\nLangChain's suite of tools –\nwith_structured_output\n,\nPydanticOutputParser\n, and\nStructuredOutputParser\n– provide powerful and flexible options for taming the wild outputs of language models.\nBy mastering these tools, you can transform the raw, unstructured potential of LLMs into structured, actionable data.\nThis not only enhances the reliability and usability of your AI applications but also opens up new possibilities for complex, data-driven workflows.\nDepending on your needs, whether it’s native structured output, type safety, or flexible schema definition, LangChain has the right tool for you.\nStart building AI applications that harness the full power of LLMs with confidence and precision today and share your experiences with us on post.\nPS: If you like this article, share it with others ♻️\nWould help a lot ❤️\nAnd feel free to follow me for articles more like this.\n#\nnlp\n#\nlangchain\n#\nmachine-learning\n#\nartificial-intelligence",
  "metadata": {
    "title": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
    "description": "Parsing structured outputs from Large Language Models (LLMs) is a crucial skill when developing AI-powered applications.\nIt allows developers to extract precise, predictable information from models, transforming unstructured text into structured data...",
    "keywords": null,
    "author": null,
    "og:title": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
    "og:description": "Parsing structured outputs from Large Language Models (LLMs) is a crucial skill when developing AI-powered applications.\nIt allows developers to extract precise, predictable information from models, transforming unstructured text into structured data...",
    "og:url": "https://juancolamendy.hashnode.dev/parsing-llm-structured-outputs-in-langchain-a-comprehensive-guide",
    "og:site_name": "Juan C Olamendy",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1729604680487/14ff49cc-c7be-494e-8cc4-3ce74657c79f.jpeg",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:creator": "https://twitter.com/juancolamendy",
    "twitter:title": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
    "twitter:description": "Parsing structured outputs from Large Language Models (LLMs) is a crucial skill when developing AI-powered applications.\nIt allows developers to extract precise, predictable information from models, transforming unstructured text into structured data...",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1729604680487/14ff49cc-c7be-494e-8cc4-3ce74657c79f.jpeg",
    "article:published_time": "2024-10-22T13:57:43.977Z",
    "article:author": "https://juancolamendy.hashnode.dev/@juancolamendy"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/15003105266b/code/body-000.txt",
      "chars": 1310,
      "language": "text"
    },
    {
      "file": "attachments/15003105266b/code/body-001.txt",
      "chars": 1541,
      "language": "text"
    },
    {
      "file": "attachments/15003105266b/code/body-002.txt",
      "chars": 1409,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1729604680487/14ff49cc-c7be-494e-8cc4-3ce74657c79f.jpeg",
      "bytes": 142730,
      "reused": true,
      "local_path": "attachments/15003105266b/images/000.jpeg",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1729604680487/14ff49cc-c7be-494e-8cc4-3ce74657c79f.jpeg",
      "alt": "Parsing LLM Structured Outputs in LangChain: A Comprehensive Guide",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://juancolamendy.hashnode.dev/parsing-llm-structured-outputs-in-langchain-a-comprehensive-guide.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@juancolamendy",
      "label": "J",
      "type": "a"
    },
    {
      "href": "https://twitter.com/juancolamendy",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/juancolamendy/",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://juancolamendy.hashnode.dev/tag/nlp",
      "label": "# nlp",
      "type": "a"
    },
    {
      "href": "https://juancolamendy.hashnode.dev/tag/langchain",
      "label": "# langchain",
      "type": "a"
    },
    {
      "href": "https://juancolamendy.hashnode.dev/tag/machine-learning",
      "label": "# machine-learning",
      "type": "a"
    },
    {
      "href": "https://juancolamendy.hashnode.dev/tag/artificial-intelligence",
      "label": "# artificial-intelligence",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:06:49",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:40Z",
  "source_url": "https://juancolamendy.hashnode.dev/parsing-llm-structured-outputs-in-langchain-a-comprehensive-guide",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 16657,
  "expected_body_images": 1,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/15003105266b/pages/source.html",
  "local_html": "attachments/15003105266b/pages/article.html",
  "body_base_path": "attachments/15003105266b/pages/",
  "body_markdown_path": "attachments/15003105266b/pages/article.md"
}