BaseOutputParser Class — langchain Architecture
Architecture documentation for the BaseOutputParser class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD e8a17d08_0c46_4355_0cc8_1a8458085683["BaseOutputParser"] d0bafc31_c5e2_3cc4_445b_1e80ff919ed1["BaseLLMOutputParser"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|extends| d0bafc31_c5e2_3cc4_445b_1e80ff919ed1 abb7c122_ee7b_4c8f_ffaa_3d3d63c4fab7["BaseMessage"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|extends| abb7c122_ee7b_4c8f_ffaa_3d3d63c4fab7 f12d594d_57eb_7d18_3649_39c89c3048be["base.py"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|defined in| f12d594d_57eb_7d18_3649_39c89c3048be 81edc411_f186_c505_b446_c7f352368a32["InputType()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 81edc411_f186_c505_b446_c7f352368a32 251046a1_0918_1dc8_282e_9a11d104db88["OutputType()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 251046a1_0918_1dc8_282e_9a11d104db88 580bdeaf_7ca0_782f_02d9_73b924f2a222["invoke()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 580bdeaf_7ca0_782f_02d9_73b924f2a222 8fa07681_9c39_6d04_399f_aba18da6aa11["ainvoke()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 8fa07681_9c39_6d04_399f_aba18da6aa11 bb47745e_84b6_0214_01a3_d6c97ab5dab7["parse_result()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| bb47745e_84b6_0214_01a3_d6c97ab5dab7 25ed0a4d_4f75_f936_c942_33cc4206746c["parse()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 25ed0a4d_4f75_f936_c942_33cc4206746c 86e356f2_7f88_4c09_b4aa_1c6dc57da65c["aparse_result()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 86e356f2_7f88_4c09_b4aa_1c6dc57da65c 5663731c_9a3f_81ad_83c5_84a0f9627d71["aparse()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 5663731c_9a3f_81ad_83c5_84a0f9627d71 a52b32fa_282d_73d3_e8a6_8c32b40161e7["parse_with_prompt()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| a52b32fa_282d_73d3_e8a6_8c32b40161e7 32d6b0e9_c92c_0a88_9600_df52109ebf99["get_format_instructions()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 32d6b0e9_c92c_0a88_9600_df52109ebf99 43b0d848_c965_53df_bcdf_fcd6f73c0019["_type()"] e8a17d08_0c46_4355_0cc8_1a8458085683 -->|method| 43b0d848_c965_53df_bcdf_fcd6f73c0019
Relationship Graph
Source Code
libs/core/langchain_core/output_parsers/base.py lines 136–348
class BaseOutputParser(
BaseLLMOutputParser, RunnableSerializable[LanguageModelOutput, T]
):
"""Base class to parse the output of an LLM call.
Output parsers help structure language model responses.
Example:
```python
# Implement a simple boolean output parser
class BooleanOutputParser(BaseOutputParser[bool]):
true_val: str = "YES"
false_val: str = "NO"
def parse(self, text: str) -> bool:
cleaned_text = text.strip().upper()
if cleaned_text not in (
self.true_val.upper(),
self.false_val.upper(),
):
raise OutputParserException(
f"BooleanOutputParser expected output value to either be "
f"{self.true_val} or {self.false_val} (case-insensitive). "
f"Received {cleaned_text}."
)
return cleaned_text == self.true_val.upper()
@property
def _type(self) -> str:
return "boolean_output_parser"
```
"""
@property
@override
def InputType(self) -> Any:
"""Return the input type for the parser."""
return str | AnyMessage
@property
@override
def OutputType(self) -> type[T]:
"""Return the output type for the parser.
This property is inferred from the first type argument of the class.
Raises:
TypeError: If the class doesn't have an inferable `OutputType`.
"""
for base in self.__class__.mro():
if hasattr(base, "__pydantic_generic_metadata__"):
metadata = base.__pydantic_generic_metadata__
if "args" in metadata and len(metadata["args"]) > 0:
return cast("type[T]", metadata["args"][0])
msg = (
f"Runnable {self.__class__.__name__} doesn't have an inferable OutputType. "
"Override the OutputType property to specify the output type."
)
raise TypeError(msg)
@override
def invoke(
self,
input: str | BaseMessage,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> T:
if isinstance(input, BaseMessage):
return self._call_with_config(
lambda inner_input: self.parse_result(
[ChatGeneration(message=inner_input)]
),
input,
config,
run_type="parser",
)
return self._call_with_config(
lambda inner_input: self.parse_result([Generation(text=inner_input)]),
Extends
Source
Frequently Asked Questions
What is the BaseOutputParser class?
BaseOutputParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/base.py.
Where is BaseOutputParser defined?
BaseOutputParser is defined in libs/core/langchain_core/output_parsers/base.py at line 136.
What does BaseOutputParser extend?
BaseOutputParser extends BaseLLMOutputParser, BaseMessage.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free