Formatting LLM Outputs with Output Parsers
By default, chat completion models return raw text. To parse these strings into structured code objects, LangChain provides Output Parsers.
1. Using StringOutputParser
The simplest parser converts the model response token chunk directly into a standard Javascript string, removing metadata wrappers:
import { StringOutputParser } from "@langchain/core/output_parsers";
const stringParser = new StringOutputParser();2. Using StructuredOutputParser with Zod
To force the model to output valid JSON matching a target structure, use StructuredOutputParser combined with Zod schemas:
// src/services/profileParser.ts
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StructuredOutputParser } from "@langchain/core/output_parsers";
import { z } from "zod";
// 1. Declare target structure using Zod
const employeeSchema = z.object({
name: z.string(),
department: z.string(),
skillsCount: z.number(),
});
// 2. Instantiate the structured parser
const employeeParser = StructuredOutputParser.fromZodSchema(employeeSchema);
// 3. Create prompt templates and inject format instructions
const prompt = ChatPromptTemplate.fromMessages([
["system", "Extract employee facts. You must match this format instructions:\n{format_instructions}"],
["user", "Facts: Mike works in Sales and knows 5 programming languages."]
]);
export async function runEmployeeParsing() {
const model = new ChatOpenAI({ modelName: "gpt-4o-mini", temperature: 0 });
// Compose chain
const chain = prompt.pipe(model).pipe(employeeParser);
// Invoke chain, passing the format instructions
const result = await chain.invoke({
format_instructions: employeeParser.getFormatInstructions(),
});
// result is typed as: { name: string; department: string; skillsCount: number }
console.log("Parsed Name:", result.name);
return result;
}3. How Parser Injection Works
When you call parser.getFormatInstructions(), LangChain generates a paragraph explaining how to structure JSON outputs (for example, instructing the model to wrap outputs in markdown JSON fences). The parser then intercepts the output and runs JSON parsing internally.
Published on Last updated: