0Pricing
AI Agents with LangChain & Autonomous Workflows · Lektion

Strukturiertes Output-Parsing und Validierung

Zwingen Sie LLMs mithilfe von Output-Parsern und Schemas dazu, zuverlässige strukturierte Daten zurückzugeben, und validieren Sie die Ausgabe oder wiederholen Sie die Anfrage, wenn das Modell fehlerhafte Daten erzeugt.

Strukturiertes Output-Parsing und Validierung ist eine kostenlose AI Agents with LangChain & Autonomous Workflows-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des AI Agents with LangChain & Autonomous Workflows-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der AI Agents with LangChain & Autonomous Workflows-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Problem with Free Text

LLMs return prose by default, but your code needs structured data: JSON, a list, a typed object. Parsing free text with regex is fragile.

This lesson covers getting reliable structured output from models.

Asking for a Format

The first step is simply instructing the model to produce a specific format. But instruction alone is not enough; models drift, add prose, or wrap output in markdown.

prompt = 'Extract name and age as JSON: "Lena is 30"'
# model might reply: 'Sure! {"name":"Lena","age":30}'

Output Parsers

LangChain output parsers do two jobs: they generate format instructions to inject into the prompt, and they parse the model's response back into a structured object.

from langchain.output_parsers import CommaSeparatedListOutputParser
parser = CommaSeparatedListOutputParser()
print(parser.get_format_instructions())

Schema-Based Parsing

Define the shape you want with a schema (e.g. a Pydantic model). The parser turns it into instructions and validates the result against the fields and types.

from pydantic import BaseModel
class Person(BaseModel):
    name: str
    age: int

Injecting Format Instructions

Add the parser's instructions into your prompt template so the model knows exactly what structure to emit.

template = 'Extract info.\n{format_instructions}\nText: {text}'
prompt = template.format(
    format_instructions=parser.get_format_instructions(),
    text='Lena is 30')

Parsing the Response

After the model replies, the parser converts the text into your typed object, raising an error if it does not match the schema.

result = parser.parse(model_output)
print(result.name, result.age)

Handling Malformed Output

Models occasionally produce invalid JSON. A retry/fixing parser detects the failure and asks the model to correct its own output, turning a hard crash into a recoverable step.

from langchain.output_parsers import RetryOutputParser
robust = RetryOutputParser.from_llm(parser=parser, llm=llm)

Native JSON / Tool Modes

Many modern models support a JSON mode or function/tool calling that constrains output to valid structured data at the API level. When available, this is far more reliable than prompt instructions alone.

Validation Beyond Types

A value can be the right type but still wrong: a negative age, an empty required field. Add validators so business rules are enforced, not just the data shape.

if result.age < 0 or result.age > 130:
    raise ValueError('age out of range')

Why It Matters for Agents

Agents chain steps together, feeding one output into the next. If a step emits malformed data, the whole chain breaks. Structured, validated output is what makes multi-step agents dependable.

A Reliable Output Workflow

Putting it together:

  • Define a schema for the data you need
  • Inject format instructions into the prompt
  • Prefer native JSON/tool mode when available
  • Parse and validate, with a retry parser as a safety net

Quick Check

Test your understanding of structured output.

Recap

You learned to get reliable structured data from LLMs.

  • Output parsers generate instructions and parse responses
  • Schemas validate shape and types
  • Retry parsers recover from malformed output
  • Native JSON/tool modes are most reliable when available

Häufig gestellte Fragen

Ist die Lektion „Strukturiertes Output-Parsing und Validierung“ kostenlos?

Ja — der vollständige Text von „Strukturiertes Output-Parsing und Validierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des AI Agents with LangChain & Autonomous Workflows-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der AI Agents with LangChain & Autonomous Workflows-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Strukturiertes Output-Parsing und Validierung“?

Zwingen Sie LLMs mithilfe von Output-Parsern und Schemas dazu, zuverlässige strukturierte Daten zurückzugeben, und validieren Sie die Ausgabe oder wiederholen Sie die Anfrage, wenn das Modell fehlerh… Du übst AI Agents with LangChain & Autonomous Workflows mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um AI Agents with LangChain & Autonomous Workflows zu starten?

Keine Vorkenntnisse erforderlich. AI Agents with LangChain & Autonomous Workflows auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Strukturiertes Output-Parsing und Validierung“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser AI Agents with LangChain & Autonomous Workflows-Lektion Code schreiben und ausführen?

Ja. Jede AI Agents with LangChain & Autonomous Workflows-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Effektive Techniken für Prompt-Design
  2. LLMs in LangChain integrieren
  3. Modellparameter und Kosten verwalten
  4. Strukturiertes Output-Parsing und Validierung
← Zurück zu AI Agents with LangChain & Autonomous Workflows