You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fromlangchain_community.chat_modelsimportChatPerplexityresearch_model=ChatPerplexity(
model="llama-3.1-sonar-large-128k-online",
temperature=0,
# to not emit messages to the clientdisable_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.
"""Wrapper around Perplexity APIs."""fromlangchain_community.chat_modelsimportChatPerplexityclassCustomChatPerplexity(ChatPerplexity):
def_generate(
self,
messages: List[BaseMessage],
stop: Optional[List[str]] =None,
run_manager: Optional[CallbackManagerForLLMRun] =None,
**kwargs: Any,
) ->ChatResult:
ifself.streaming:
stream_iter=self._stream(
messages, stop=stop, run_manager=run_manager, **kwargs
)
ifstream_iter:
returngenerate_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 returnedmessage=AIMessage(
content=response.choices[0].message.content,
additional_kwargs={"citations": response.citations},
)
returnChatResult(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.
Checked other resources
Example Code
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
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: