Working with the SelfAskOutputParser in LangChain
Posted: Jan 29, 2025.
The SelfAskOutputParser is a specialized output parser in LangChain designed to handle the output of self-ask style conversations with language models. In this guide, we'll explore what it is, how it works, and how to use it effectively.
What is SelfAskOutputParser?
SelfAskOutputParser is an output parser that handles responses from language models in a self-ask conversation format. It specifically looks for two types of patterns:
- Follow-up questions that indicate more information is needed
- Final answers that conclude the conversation
The parser converts the LLM's output into either an AgentAction
(when a follow-up is needed) or an AgentFinish
(when a final answer is provided).
Reference
Here are the key components of the SelfAskOutputParser:
Parameter | Type | Description |
---|---|---|
finish_string | str | The string that indicates a final answer is being given (default: "So the final answer is: ") |
followups | Sequence[str] | The strings that indicate a follow-up question (default: ["Follow up:", "Followup:"]) |
How to use SelfAskOutputParser
Basic Usage
Here's how to create and use a SelfAskOutputParser:
Customizing the Parser
You can customize the strings that indicate follow-ups or final answers:
Integration with an Agent
The SelfAskOutputParser is typically used as part of an agent that needs to break down complex questions into simpler ones:
Error Handling
The parser will raise an error if it can't find either a follow-up question or a final answer in the text:
Remember that the SelfAskOutputParser expects the LLM output to follow a specific format. Make sure your prompts guide the LLM to provide responses in the correct format with either follow-up questions or final answers clearly marked.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.