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

ChatPerplexity: 'str' object has no attribute 'choices' #29254

Open
5 tasks done
n-sviridenko opened this issue Jan 16, 2025 · 1 comment
Open
5 tasks done

ChatPerplexity: 'str' object has no attribute 'choices' #29254

n-sviridenko opened this issue Jan 16, 2025 · 1 comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@n-sviridenko
Copy link

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
research_model = ChatPerplexity(
        model="llama-3.1-sonar-large-128k-online",
        temperature=0,
        # to not emit messages to the client
        disable_streaming=True,
    )

research_prompt = 'How many starts are there?'
research_response = research_model.invoke([HumanMessage(content=research_prompt)])

Error Message and Stack Trace (if applicable)

AttributeError("'str' object has no attribute 'choices'")Traceback (most recent call last):

File "/Users/nsviridenko/.local/share/virtualenvs/nce-agent-dIPyehe6/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 633, in generate
self._generate_with_cache(

File "/Users/nsviridenko/.local/share/virtualenvs/nce-agent-dIPyehe6/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 851, in _generate_with_cache
result = self._generate(
^^^^^^^^^^^^^^^

File "/Users/nsviridenko/.local/share/virtualenvs/nce-agent-dIPyehe6/lib/python3.11/site-packages/langchain_community/chat_models/perplexity.py", line 265, in _generate
content=response.choices[0].message.content,
^^^^^^^^^^^^^^^^

AttributeError: 'str' object has no attribute 'choices'

Description

When trying to use ChatPerplexity, it throws an error.

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 20.6.0: Tue Jun 21 20:50:28 PDT 2022; root:xnu-7195.141.32~1/RELEASE_X86_64
Python Version: 3.11.5 (main, Sep 29 2024, 15:09:05) [Clang 12.0.5 (clang-1205.0.22.11)]

Package Information

langchain_core: 0.3.21
langchain: 0.3.9
langchain_community: 0.3.9
langsmith: 0.1.147
langchain_anthropic: 0.3.0
langchain_openai: 0.2.10
langchain_text_splitters: 0.3.2
langgraph_api: 0.0.15
langgraph_cli: 0.1.65
langgraph_license: Installed. No version info available.
langgraph_sdk: 0.1.48
langgraph_storage: Installed. No version info available.

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.11
anthropic: 0.42.0
async-timeout: Installed. No version info available.
click: 8.1.8
cryptography: 43.0.3
dataclasses-json: 0.6.7
defusedxml: 0.7.1
httpx: 0.28.1
httpx-sse: 0.4.0
jsonpatch: 1.33
jsonschema-rs: 0.25.1
langgraph: 0.2.56
langgraph-checkpoint: 2.0.9
langsmith-pyo3: Installed. No version info available.
numpy: 1.26.4
openai: 1.55.3
orjson: 3.10.13
packaging: 24.2
pydantic: 2.10.4
pydantic-settings: 2.7.1
pyjwt: 2.10.1
python-dotenv: 1.0.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.27
sse-starlette: 2.1.3
starlette: 0.45.2
structlog: 24.4.0
tenacity: 8.5.0
tiktoken: 0.8.0
typing-extensions: 4.12.2
uvicorn: 0.34.0
watchfiles: 1.0.3

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jan 16, 2025
@keenborder786
Copy link
Contributor

@n-sviridenko Can you use the following class:

"""Wrapper around Perplexity APIs."""
from langchain_community.chat_models import ChatPerplexity 

class CustomChatPerplexity(ChatPerplexity):
    
    def _generate(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> ChatResult:
        if self.streaming:
            stream_iter = self._stream(
                messages, stop=stop, run_manager=run_manager, **kwargs
            )
            if stream_iter:
                return generate_from_stream(stream_iter)
        message_dicts, params = self._create_message_dicts(messages, stop)
        params = {**params, **kwargs}
        response = self.client.chat.completions.create(messages=message_dicts, **params)
        print(response) # Outputing the response to see what is being returned
        message = AIMessage(
            content=response.choices[0].message.content,
            additional_kwargs={"citations": response.citations},
        )
        return ChatResult(generations=[ChatGeneration(message=message)])

Because according to official Preplexity documentation: https://docs.perplexity.ai/api-reference/chat-completions. The response should be a json. Therefore I wanna see the output of the response that you are getting, before I can help you.

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
Development

No branches or pull requests

2 participants