Home / File/ output_parsers.py — langchain Source File

output_parsers.py — langchain Source File

Architecture documentation for output_parsers.py, a python file in the langchain codebase. 5 imports, 0 dependents.

File python LangChainCore ApiManagement 5 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  1be78f27_fe4d_35ef_90f6_0cba68eb107b["output_parsers.py"]
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  1be78f27_fe4d_35ef_90f6_0cba68eb107b --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  1be78f27_fe4d_35ef_90f6_0cba68eb107b --> feec1ec4_6917_867b_d228_b134d0ff8099
  628cbc5d_711f_ac0c_2f53_db992d48d7da["langchain_core.output_parsers"]
  1be78f27_fe4d_35ef_90f6_0cba68eb107b --> 628cbc5d_711f_ac0c_2f53_db992d48d7da
  4382dc25_6fba_324a_49e2_e9742d579385["langchain_core.outputs"]
  1be78f27_fe4d_35ef_90f6_0cba68eb107b --> 4382dc25_6fba_324a_49e2_e9742d579385
  314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7["langchain_core.utils.pydantic"]
  1be78f27_fe4d_35ef_90f6_0cba68eb107b --> 314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7
  style 1be78f27_fe4d_35ef_90f6_0cba68eb107b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re
from typing import Any, Generic

from langchain_core.output_parsers import JsonOutputParser, PydanticOutputParser
from langchain_core.outputs import Generation
from langchain_core.utils.pydantic import TBaseModel


def strip_think_tags(text: str) -> str:
    """Removes all <think>...</think> tags and their content from text.

    This function removes all occurrences of think tags, preserving text
    before, between, and after the tags. It also handles markdown code fences.

    Args:
        text: The input text that may contain think tags.

    Returns:
        The text with all `<think>...</think>` blocks removed.
    """
    # Remove all <think>...</think> blocks using regex
    # The pattern matches <think> followed by any content (non-greedy) until </think>
    result = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)

    # Remove markdown code fence markers if present
    result = result.strip()
    if result.startswith("```json"):
        result = result[len("```json") :].strip()
    elif result.startswith("```"):
        result = result[3:].strip()

    if result.endswith("```"):
        result = result[:-3].strip()

    return result


class ReasoningJsonOutputParser(JsonOutputParser):
    """A JSON output parser that strips reasoning tags before parsing.

    This parser removes any content enclosed in <think> tags from the input text
    before delegating to the parent JsonOutputParser for JSON parsing.

    """

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a JSON object.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON objects.
                If `True`, the output will be a JSON object containing
                all the keys that have been returned so far.
                If `False`, the output will be the full JSON object.

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        text = result[0].text
        text = strip_think_tags(text)
        return super().parse_result([Generation(text=text)], partial=partial)


class ReasoningStructuredOutputParser(
    PydanticOutputParser[TBaseModel], Generic[TBaseModel]
):
    """A structured output parser that strips reasoning tags before parsing.

    This parser removes any content enclosed in <think> tags from the input text
    before delegating to the parent PydanticOutputParser for structured parsing.
    """

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a Pydantic object.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON objects.
                If `True`, the output will be a JSON object containing
                all the keys that have been returned so far.
                If `False`, the output will be the full JSON object.
        """
        text = result[0].text
        text = strip_think_tags(text)
        return super().parse_result([Generation(text=text)], partial=partial)

Domain

Subdomains

Functions

Dependencies

  • langchain_core.output_parsers
  • langchain_core.outputs
  • langchain_core.utils.pydantic
  • re
  • typing

Frequently Asked Questions

What does output_parsers.py do?
output_parsers.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in output_parsers.py?
output_parsers.py defines 1 function(s): strip_think_tags.
What does output_parsers.py depend on?
output_parsers.py imports 5 module(s): langchain_core.output_parsers, langchain_core.outputs, langchain_core.utils.pydantic, re, typing.
Where is output_parsers.py in the architecture?
output_parsers.py is located at libs/partners/perplexity/langchain_perplexity/output_parsers.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/partners/perplexity/langchain_perplexity).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free