Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from OpenAI Client to Perplexity API for Full Functionality #30393

Closed
5 tasks done
DavidSanSan110 opened this issue Mar 20, 2025 · 4 comments
Closed
5 tasks done
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@DavidSanSan110
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • 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).

Example Code

from langchain_community.chat_models import ChatPerplexity

chat_perplexity = ChatPerplexity(model="sonar-pro", temperature=0.8)

# Generate with search_recency_filter
response = chat_perplexity.invoke("Tell me about Michael Jordan.", search_recency_filter="week")

Error Message and Stack Trace (if applicable)

TypeError: Completions.create() got an unexpected keyword argument 'search_recency_filter'

Description

The latest version of LangChain uses the OpenAI client to make calls to Perplexity. However, the OpenAI client does not support certain Perplexity-specific attributes such as search_domain_filter and search_recency_filter. This results in errors when trying to use these parameters, even though Perplexity does support them.

System Info

System Information

OS: Windows
OS Version: 10.0.26100
Python Version: 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]

Package Information

langchain_core: 0.3.41
langchain: 0.3.20
langchain_community: 0.3.19
langsmith: 0.3.1
langchain_openai: 0.3.2
langchain_text_splitters: 0.3.6

Optional packages not installed

langserve

Other Dependencies

aiohttp<4.0.0,>=3.8.3: Installed. No version info available.
async-timeout<5.0.0,>=4.0.0;: Installed. No version info available.
dataclasses-json<0.7,>=0.5.7: Installed. No version info available.
httpx: 0.28.1
httpx-sse<1.0.0,>=0.4.0: Installed. No version info available.
jsonpatch<2.0,>=1.33: Installed. No version info available.
langchain-anthropic;: Installed. No version info available.
langchain-aws;: Installed. No version info available.
langchain-cohere;: Installed. No version info available.
langchain-community;: Installed. No version info available.
langchain-core<1.0.0,>=0.3.34: Installed. No version info available.
langchain-core<1.0.0,>=0.3.41: Installed. No version info available.
langchain-deepseek;: Installed. No version info available.
langchain-fireworks;: Installed. No version info available.
langchain-google-genai;: Installed. No version info available.
langchain-google-vertexai;: Installed. No version info available.
langchain-groq;: Installed. No version info available.
langchain-huggingface;: Installed. No version info available.
langchain-mistralai;: Installed. No version info available.
langchain-ollama;: Installed. No version info available.
langchain-openai;: Installed. No version info available.
langchain-text-splitters<1.0.0,>=0.3.6: Installed. No version info available.
langchain-together;: Installed. No version info available.
langchain-xai;: Installed. No version info available.
langchain<1.0.0,>=0.3.20: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
langsmith<0.4,>=0.1.17: Installed. No version info available.
numpy<3,>=1.26.2: Installed. No version info available.
openai: 1.60.0
orjson: 3.10.15
packaging<25,>=23.2: Installed. No version info available.
pydantic: 2.10.6
pydantic-settings<3.0.0,>=2.4.0: Installed. No version info available.
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
pytest: 8.3.4
PyYAML>=5.3: Installed. No version info available.
requests: 2.32.3
requests-toolbelt: 1.0.0
requests<3,>=2: Installed. No version info available.
rich: 13.9.4
SQLAlchemy<3,>=1.4: Installed. No version info available.
tenacity!=8.4.0,<10,>=8.1.0: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken: 0.8.0
typing-extensions>=4.7: Installed. No version info available.
zstandard: 0.23.0

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Mar 20, 2025
@jaimepcr
Copy link

I ran into the same issue and couldn’t use the full Perplexity functionality in my requests. It would be great if this could be supported! Definitely think this would be a useful update. 👌

@ccurme
Copy link
Collaborator

ccurme commented Mar 22, 2025

The OpenAI client supports some fields for handling additional parameters, see for example here. Can we use these?

from langchain_community.chat_models import ChatPerplexity

chat_perplexity = ChatPerplexity(model="sonar-pro", temperature=0.8)

response = chat_perplexity.invoke(
    "Tell me about Michael Jordan.",
    extra_body={"search_recency_filter": "week"},
)

I tried passing {"search_recency_filter": "invalid"} and got a reasonable error message:

BadRequestError: Error code: 400 - {'error': {'message': "Validation error: search_recency_filter must be either 'hour', 'day', 'week', 'month', or 'year'", 'type': 'invalid_search_recency_filter', 'code': 400}}

@DavidSanSan110
Copy link
Contributor Author

DavidSanSan110 commented Mar 23, 2025

Thank you for noticing! I tried it, and it works as expected.

However, I also noticed something else in the Python code block:

message = AIMessage(
    content=response.choices[0].message.content,
    additional_kwargs={"citations": response.citations},
    usage_metadata=usage_metadata,
)

Currently, only citations are appended to the response, but depending on the parameters passed, the response could include other important attributes like related_questions or images. To ensure the Perplexity response is handled correctly, these fields should also be included when available.

I’m working on a PR to properly append or return these fields. Let me know if you have any thoughts on this!

Should I close the other PR?

@ccurme
Copy link
Collaborator

ccurme commented Mar 23, 2025

Thanks @DavidSanSan110, will take a look at the new PR.

For context, we will likely move ChatPerplexity into a dedicated package langchain-perplexity here in the next few weeks. So in the current implementation in langchain-community, I'd limit updates to bug fixes and small / straightforward feature additions (the new PR looks reasonable but will take a closer look-- we're a backlogged at the moment).

In my mind here are some other needs for ChatPerplexity:

  1. We should document how to access search_recency_filter, search_domain_filter, etc. in our docs page here.
  2. If search_recency_filter, search_domain_filter, etc. make sense as initialization parameters, we can add them to ChatPerplexity so that they will be more visible and easier to access.
  3. We should fill out its docstring so that its API Referece is populated. See example here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
3 participants