Skip to content

Chapter 2 of 8

Prompts, Chat Models, and Output Parsers

Prompts are how you talk to the model, and LangChain offers a small family of templates that interpolate variables, hold few-shot examples, or inject dynamic message lists. The simplest is PromptTemplate, which formats a single string for completion-style models; you build one with the from_template factory and invoke it with a dict of variable values. ChatPromptTemplate is the chat equivalent: constructed with from_messages, it accepts a list of role and template tuples (such as system and human), where variables are wrapped in curly braces. FewShotPromptTemplate layers on top to inject a list of example input and output pairs at runtime, letting the model learn the desired pattern via in-context examples rather than fine-tuning. MessagesPlaceholder is a special template entry that inserts a dynamic list of messages — most often the chat history — into a chat prompt at invocation time. All of these ultimately produce a PromptValue, which exposes both to_string and to_messages methods so the same template can feed either a string LLM or a chat model without rewriting it.

Chat models are invoked with invoke on a list of messages or a prompt value, returning an AIMessage. Parameters like model name, temperature, max_tokens, and request_timeout are passed to the constructor, and bind lets you freeze per-call overrides — such as a higher temperature, a stop sequence, or a tools list — onto a new runnable without rebuilding the chain. For streaming, calling stream returns an iterator of chunks; for batched processing, batch accepts a list of inputs and supports a max_concurrency config option. Async variants integrate with Python's asyncio for parallel or long-running pipelines. For OpenAI-style models, with_structured_output is a unified wrapper that binds a Pydantic model or JSON schema to the LLM so the response comes back as validated structured data instead of free-form text, eliminating the need to write parsing prompts by hand.

Output parsers sit at the end of a chain to convert the model's raw output into a more useful type. StrOutputParser extracts the plain string from an AIMessage and is the most common parser in LCEL chains. JsonOutputParser and PydanticOutputParser parse the response into a Python dict or a validated Pydantic model instance; both expose get_format_instructions so you can inject a description of the expected schema into the prompt, and PydanticOutputParser adds type coercion and explicit error messages. Specialized parsers cover other shapes: CommaSeparatedListOutputParser returns a list of strings, DatetimeOutputParser uses a format string to produce a Python datetime, EnumOutputParser validates that the model picked one of a fixed set of values, XMLOutputParser traverses an XML schema into a dict, and StructuredOutputParser handles a list of named string response schemas returning a dict without strict type coercion.

When the LLM fails to follow a schema, OutputFixingParser wraps a base parser and uses a second LLM call to repair the output, while RetryWithErrorOutputParser re-prompts with both the original output and the parser's specific error message, giving the model tighter feedback. Both rescue a chain from total failure on the first malformed response. The format_instructions pattern — injecting the parser's get_format_instructions string into the prompt via partial_variables or prompt.partial — is the standard way to keep the model aware of the expected output shape, and it composes cleanly with with_structured_output for native provider support where available.

All chapters
  1. 1Foundations of LangChain and the Runnable Interface
  2. 2Prompts, Chat Models, and Output Parsers
  3. 3Composing Runnables and Controlling Execution
  4. 4Memory and Conversation History
  5. 5Documents, Embeddings, and Vector Stores
  6. 6RAG Pipelines and Document Processing
  7. 7Tools, Agents, and LangGraph
  8. 8Observability, Evaluation, Deployment, and Caching

Drill it

Reading is not remembering. These come from the LangChain deck:

Q

What is LangChain?

LangChain is an open-source Python framework for building applications powered by large language models (LLMs). It provides modular components for prompts, chai...

Q

What is LCEL (LangChain Expression Language)?

LCEL is a declarative syntax for composing LangChain components using the pipe operator (|). It creates RunnableSequence chains that support streaming, batching...

Q

How do you create a ChatPromptTemplate?

Use ChatPromptTemplate.from_messages() with a list of (role, template) tuples. Variables are wrapped in curly braces.from langchain_core.prompts import ChatProm...

Q

How do you invoke a ChatModel in LangChain?

Instantiate the model class and call .invoke() with a list of messages or a prompt value. The model returns an AIMessage.from langchain_openai import ChatOpenAI...