Skip to content

Perplexity Reasoning model with structuredOutput causes JsonOutputParser errorΒ #32465

@tgrimminger

Description

@tgrimminger

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Example Code

from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field

class Answer(BaseModel):
    number: int = Field(...)

llm = init_chat_model("sonar-reasoning-pro", model_provider="perplexity", api_key="your-key")
prompt = ChatPromptTemplate.from_template("Question: {question}")
chain = prompt | llm.with_structured_output(Answer)
result = chain.invoke({"question": "Test question"})

Error Message and Stack Trace (if applicable)

The structured output does not get parsed correctly, because the perplexity API includes the reasoning process in the output, i.e.: Invalid json output: <think>[...]</think>{ "number": 2 }

Description

I'm using langchain to get structured output from perplexity reasoning models.

I expect the output to be parsed correctly as it is formatted correctly as per perplexities API docs.

This issue has actually been solved in the JS package already. So we would only need to port this to the python side, if I understand correctly.

System Info

aiohappyeyeballs==2.6.1
aiohttp==3.12.15
aiosignal==1.4.0
annotated-types==0.7.0
anthropic==0.61.0
anyio==4.9.0
attrs==25.3.0
cachetools==5.5.2
certifi==2025.8.3
charset-normalizer==3.4.2
distro==1.9.0
et_xmlfile==2.0.0
filetype==1.2.0
frozenlist==1.7.0
google-ai-generativelanguage==0.6.18
google-api-core==2.25.1
google-auth==2.40.3
googleapis-common-protos==1.70.0
groq==0.31.0
grpcio==1.74.0
grpcio-status==1.74.0
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
jiter==0.10.0
jsonpatch==1.33
jsonpointer==3.0.0
langchain==0.3.27
langchain-anthropic==0.3.18
langchain-core==0.3.72
langchain-deepseek==0.1.4
langchain-google-genai==2.1.9
langchain-groq==0.3.7
langchain-openai==0.3.28
langchain-perplexity==0.1.2
langchain-text-splitters==0.3.9
langchain-xai==0.2.5
langsmith==0.4.10
multidict==6.6.3
numpy==2.3.2
openai==1.98.0
openpyxl==3.1.5
orjson==3.11.1
packaging==25.0
pandas==2.3.1
propcache==0.3.2
proto-plus==1.26.1
protobuf==6.31.1
pyasn1==0.6.1
pyasn1_modules==0.4.2
pydantic==2.11.7
pydantic_core==2.33.2
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.2
regex==2025.7.34
requests==2.32.4
requests-toolbelt==1.0.0
rsa==4.9.1
six==1.17.0
sniffio==1.3.1
SQLAlchemy==2.0.42
tenacity==9.1.2
tiktoken==0.9.0
tqdm==4.67.1
typing-inspection==0.4.1
typing_extensions==4.14.1
tzdata==2025.2
urllib3==2.5.0
yarl==1.20.1
zstandard==0.23.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions