0Pricing
LangChain / RAG / Vector DBs · Lekcja

Tworzenie niestandardowych parserów wyników

Przekształcaj surowy tekst LLM w niezawodne dane strukturalne za pomocą niestandardowych i wbudowanych parserów wyników LangChain.

Tworzenie niestandardowych parserów wyników to bezpłatna lekcja LangChain / RAG / Vector DBs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej LangChain / RAG / Vector DBs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 type
  • get_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 n

Putting 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 parse and get_format_instructions
  • Use PydanticOutputParser for rich schemas
  • Pipe parsers into chains with |
  • Wrap with OutputFixingParser for resilience

Często zadawane pytania

Czy lekcja „Tworzenie niestandardowych parserów wyników” jest bezpłatna?

Tak — pełny tekst „Tworzenie niestandardowych parserów wyników” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu LangChain / RAG / Vector DBs, przejdź na CoddyKit PRO. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Co nauczysz się w „Tworzenie niestandardowych parserów wyników”?

Przekształcaj surowy tekst LLM w niezawodne dane strukturalne za pomocą niestandardowych i wbudowanych parserów wyników LangChain. Ćwiczysz LangChain / RAG / Vector DBs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć LangChain / RAG / Vector DBs?

Nie wymagamy żadnego doświadczenia. LangChain / RAG / Vector DBs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Tworzenie niestandardowych parserów wyników”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji LangChain / RAG / Vector DBs?

Tak. Każda lekcja LangChain / RAG / Vector DBs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Tworzenie niestandardowych loaderów dokumentów
  2. Integracja niestandardowych modeli embeddingów
  3. Rozszerzanie łańcuchów wyszukiwania o niestandardową logikę
  4. Tworzenie niestandardowych parserów wyników
← Powrót do LangChain / RAG / Vector DBs