0
0
LangchainHow-ToBeginner ยท 4 min read

How to Use with_structured_output in Langchain for Structured Responses

In Langchain, with_structured_output is used to wrap a prompt or chain to enforce structured output formats like JSON. It helps parse the model's response into defined data structures automatically for easier handling.
๐Ÿ“

Syntax

The with_structured_output function wraps a Langchain prompt or chain to specify the expected output format. It takes a schema argument defining the structure, and returns a new chain that parses the output accordingly.

Key parts:

  • schema: Defines the expected output format, often using Pydantic models or Langchain's output parsers.
  • Wrapped chain or prompt: The original chain or prompt to enforce structured output on.
python
from pydantic import BaseModel
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.output_parsers import with_structured_output

class Person(BaseModel):
    name: str
    age: int

prompt = PromptTemplate(template="""Give me a person's name and age in JSON format.""")

llm_chain = LLMChain(prompt=prompt, llm=some_llm)

structured_chain = with_structured_output(llm_chain, schema=Person)
๐Ÿ’ป

Example

This example shows how to create a Langchain chain that returns a structured Person object with name and age fields parsed automatically from the model's JSON output.

python
from pydantic import BaseModel
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.output_parsers import with_structured_output
from langchain.llms import OpenAI

class Person(BaseModel):
    name: str
    age: int

prompt = PromptTemplate(template="""Provide a person's name and age in JSON format.""")

llm = OpenAI(temperature=0)
llm_chain = LLMChain(prompt=prompt, llm=llm)

structured_chain = with_structured_output(llm_chain, schema=Person)

result = structured_chain.run()
print(result)
Output
{"name": "Alice", "age": 30}
โš ๏ธ

Common Pitfalls

Common mistakes when using with_structured_output include:

  • Not defining the schema correctly, causing parsing errors.
  • Expecting the model to always produce perfectly formatted JSON; sometimes the output needs prompt tuning.
  • Using incompatible schema types that the parser cannot handle.
  • Forgetting to wrap the original chain or prompt with with_structured_output, so output remains unstructured.
python
from pydantic import BaseModel

# Wrong: schema missing required fields or wrong types
class BadSchema(BaseModel):
    fullname: int  # Should be str

# Right: correct schema
class GoodSchema(BaseModel):
    fullname: str

# Use with_structured_output with GoodSchema, not BadSchema
๐Ÿ“Š

Quick Reference

Tips for using with_structured_output:

  • Define clear Pydantic models for your expected output.
  • Use prompt templates that instruct the model to respond in JSON.
  • Test outputs and adjust prompts if parsing fails.
  • Wrap your chain or prompt with with_structured_output to get parsed objects.
โœ…

Key Takeaways

Use with_structured_output to enforce and parse structured responses from language models.
Define your output schema clearly using Pydantic models for best results.
Wrap your existing Langchain chain or prompt with with_structured_output to get parsed objects.
Ensure your prompt instructs the model to output JSON matching your schema.
Test and refine prompts to avoid parsing errors from unexpected output formats.