Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain Perplexity Output Parsers

From Leeroopedia
Knowledge Sources
Domains LLM, Output Parsing, Perplexity
Last Updated 2026-02-11 00:00 GMT

Overview

Output parsers for Perplexity LLM responses that strip reasoning <think> tags before delegating to standard JSON and Pydantic parsers.

Description

This module, part of the langchain-perplexity partner package, provides specialized output parsers that handle Perplexity model responses containing chain-of-thought reasoning enclosed in <think>...</think> tags. It includes a helper function strip_think_tags and two parser classes: ReasoningJsonOutputParser (extends JsonOutputParser) and ReasoningStructuredOutputParser (extends PydanticOutputParser). Both parsers strip reasoning tags and markdown code fences before passing the cleaned text to their parent parsers.

Usage

Import these parsers when working with Perplexity models that produce chain-of-thought reasoning in <think> blocks and you need to extract structured JSON or Pydantic-validated output from the response.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/perplexity/langchain_perplexity/output_parsers.py
  • Lines: 1-88

Signature

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

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

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        ...

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

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        ...

Import

from langchain_perplexity.output_parsers import (
    strip_think_tags,
    ReasoningJsonOutputParser,
    ReasoningStructuredOutputParser,
)

I/O Contract

strip_think_tags

Inputs

Name Type Required Description
text str Yes The input text that may contain <think>...</think> tags and markdown code fences.

Outputs

Name Type Description
return str The text with all <think>...</think> blocks and markdown code fences removed.

ReasoningJsonOutputParser.parse_result

Inputs

Name Type Required Description
result list[Generation] Yes The result of the LLM call containing generated text.
partial bool No Whether to parse partial JSON objects. Defaults to False.

Outputs

Name Type Description
return Any The parsed JSON object with reasoning tags stripped.

ReasoningStructuredOutputParser.parse_result

Inputs

Name Type Required Description
result list[Generation] Yes The result of the LLM call containing generated text.
partial bool No Whether to parse partial JSON objects. Defaults to False.

Outputs

Name Type Description
return Any The parsed Pydantic model instance with reasoning tags stripped.

Usage Examples

Basic Usage

from langchain_perplexity.output_parsers import (
    strip_think_tags,
    ReasoningJsonOutputParser,
)

# Strip think tags from raw text
raw = "<think>Let me reason about this...</think>{\"name\": \"Alice\"}"
cleaned = strip_think_tags(raw)
# cleaned == '{"name": "Alice"}'

# Use the parser in a chain
parser = ReasoningJsonOutputParser()
# parser can be used as part of a LangChain chain:
# chain = llm | parser

Structured Output with Pydantic

from pydantic import BaseModel
from langchain_perplexity.output_parsers import ReasoningStructuredOutputParser

class UserInfo(BaseModel):
    name: str
    age: int

parser = ReasoningStructuredOutputParser(pydantic_object=UserInfo)
# Use in a chain with a Perplexity model that outputs reasoning tags
# chain = llm | parser

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment