Eigene Output-Parser erstellen
Wandeln Sie rohen LLM-Text mit eigenen und integrierten LangChain-Output-Parsern in verlässliche strukturierte Daten um.
Eigene Output-Parser erstellen ist eine kostenlose LangChain / RAG / Vector DBs-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 LangChain / RAG / Vector DBs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der LangChain / RAG / Vector DBs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Parse Output?
LLMs return free-form text, but applications need structured data: objects, lists, enums. An output parser converts the raw string into something your code can use safely.
The Parser Interface
A LangChain output parser implements two key methods.
parse(text)turns the string into your typeget_format_instructions()tells the model how to format its reply
A Minimal Custom Parser
Subclass BaseOutputParser and implement parse. This one splits a comma-separated reply into a list.
from langchain_core.output_parsers import BaseOutputParser
class CommaListParser(BaseOutputParser):
def parse(self, text):
return [t.strip() for t in text.split(",")]
print(CommaListParser().parse("a, b, c")) # ["a", "b", "c"]Format Instructions
Override get_format_instructions so the prompt nudges the model toward a parseable shape. The instructions are injected into your prompt template.
def get_format_instructions(self):
return "Reply with items separated by commas, no numbering."Pydantic Output Parser
For rich objects, LangChain ships a PydanticOutputParser. You define a schema and it generates instructions plus validation automatically.
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
# parser = PydanticOutputParser(pydantic_object=Person)Wiring a Parser into a Chain
With LCEL you pipe the model output straight into the parser using the | operator.
chain = prompt | llm | CommaListParser()
result = chain.invoke({"topic": "fruits"})Handling Malformed Output
Models sometimes ignore the format. A robust parser validates and raises a clear error, or attempts a best-effort recovery, instead of crashing downstream.
def parse(self, text):
try:
return json.loads(text)
except json.JSONDecodeError:
raise ValueError("Model did not return valid JSON")The OutputFixingParser
Wrap any parser in an OutputFixingParser. When parsing fails, it sends the bad output and the error back to an LLM to repair it.
Streaming and Parsers
Some parsers support incremental parsing of streamed tokens. For structured types this is hard, so streaming parsers often emit partial objects as fields complete.
Validation Beyond Types
You can add business rules inside parse: ranges, allowed values, required combinations. Reject anything that would corrupt your application state.
def parse(self, text):
n = int(text.strip())
if not 1 <= n <= 5:
raise ValueError("Rating must be 1-5")
return nPutting It Together
Define the parser, expose format instructions, inject them into the prompt, and pipe everything in a chain. The result is reliable structured output.
prompt = template.partial(
format=parser.get_format_instructions()
)
chain = prompt | llm | parser
chain.invoke({"input": "..."})Quick Check
Test your understanding of output parsers.
Recap
You built custom output parsing:
- Implement
parseandget_format_instructions - Use
PydanticOutputParserfor rich schemas - Pipe parsers into chains with
| - Wrap with
OutputFixingParserfor resilience
Häufig gestellte Fragen
Ist die Lektion „Eigene Output-Parser erstellen“ kostenlos?
Ja — der vollständige Text von „Eigene Output-Parser erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des LangChain / RAG / Vector DBs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der LangChain / RAG / Vector DBs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Eigene Output-Parser erstellen“?
Wandeln Sie rohen LLM-Text mit eigenen und integrierten LangChain-Output-Parsern in verlässliche strukturierte Daten um. Du übst LangChain / RAG / Vector DBs 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 LangChain / RAG / Vector DBs zu starten?
Keine Vorkenntnisse erforderlich. LangChain / RAG / Vector DBs 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 „Eigene Output-Parser erstellen“?
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 LangChain / RAG / Vector DBs-Lektion Code schreiben und ausführen?
Ja. Jede LangChain / RAG / Vector DBs-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
- Entwicklung benutzerdefinierter Dokument-Loader
- Integration benutzerdefinierter Embedding-Modelle
- Erweiterung von Retrieval-Ketten mit eigener Logik
- Eigene Output-Parser erstellen