Skip to content

The agent engineering platform.

community: fix perplexity response parameters not being included in model response (#30440)

David Sánchez Sánchez75823d580b0f
diff --git a/libs/community/langchain_community/chat_models/perplexity.py b/libs/community/langchain_community/chat_models/perplexity.py
index 3ddba312ea..fd9b735f4f 100644
--- a/libs/community/langchain_community/chat_models/perplexity.py
+++ b/libs/community/langchain_community/chat_models/perplexity.py
@@ -345,16 +345,25 @@ class ChatPerplexity(BaseChatModel):
if len(chunk["choices"]) == 0:
continue
choice = chunk["choices"][0]
- citations = chunk.get("citations", [])
+
+ additional_kwargs = {}
+ if first_chunk:
+ additional_kwargs["citations"] = chunk.get("citations", [])
+ for attr in ["images", "related_questions"]:
+ if attr in chunk:
+ additional_kwargs[attr] = chunk[attr]
chunk = self._convert_delta_to_message_chunk(
choice["delta"], default_chunk_class
)
+
if isinstance(chunk, AIMessageChunk) and usage_metadata:
chunk.usage_metadata = usage_metadata
+
if first_chunk:
- chunk.additional_kwargs |= {"citations": citations}
+ chunk.additional_kwargs |= additional_kwargs
first_chunk = False
+
finish_reason = choice.get("finish_reason")
generation_info = (
dict(finish_reason=finish_reason) if finish_reason is not None else None
@@ -386,9 +395,14 @@ class ChatPerplexity(BaseChatModel):
else:
usage_metadata = None
+ additional_kwargs = {"citations": response.citations}
+ for attr in ["images", "related_questions"]:
+ if hasattr(response, attr):
+ additional_kwargs[attr] = getattr(response, attr)
+
message = AIMessage(
content=response.choices[0].message.content,
- additional_kwargs={"citations": response.citations},
+ additional_kwargs=additional_kwargs,
usage_metadata=usage_metadata,
)
return ChatResult(generations=[ChatGeneration(message=message)])
diff --git a/libs/community/tests/unit_tests/chat_models/test_perplexity.py b/libs/community/tests/unit_tests/chat_models/test_perplexity.py
index c5a745cbce..45cc4ec7cd 100644
--- a/libs/community/tests/unit_tests/chat_models/test_perplexity.py
+++ b/libs/community/tests/unit_tests/chat_models/test_perplexity.py
@@ -116,3 +116,160 @@ def test_perplexity_stream_includes_citations(mocker: MockerFixture) -> None:
assert full.additional_kwargs == {"citations": ["example.com", "example2.com"]}
patcher.assert_called_once()
+
+
+def test_perplexity_stream_includes_citations_and_images(mocker: MockerFixture) -> None:
+ """Test that the stream method includes citations in the additional_kwargs."""
+ llm = ChatPerplexity(
+ model="test",
+ timeout=30,
+ verbose=True,
+ )
+ mock_chunk_0 = {
+ "choices": [
+ {
+ "delta": {
+ "content": "Hello ",
+ },
+ "finish_reason": None,
+ }
+ ],
+ "citations": ["example.com", "example2.com"],
+ "images": [
+ {
+ "image_url": "mock_image_url",
+ "origin_url": "mock_origin_url",
+ "height": 100,
+ "width": 100,
+ }
+ ],
+ }
+ mock_chunk_1 = {
+ "choices": [
+ {
+ "delta": {
+ "content": "Perplexity",
+ },
+ "finish_reason": None,
+ }
+ ],
+ "citations": ["example.com", "example2.com"],
+ "images": [
+ {
+ "image_url": "mock_image_url",
+ "origin_url": "mock_origin_url",
+ "height": 100,
+ "width": 100,
+ }
+ ],
+ }
+ mock_chunks: List[Dict[str, Any]] = [mock_chunk_0, mock_chunk_1]
+ mock_stream = MagicMock()
+ mock_stream.__iter__.return_value = mock_chunks
+ patcher = mocker.patch.object(
+ llm.client.chat.completions, "create", return_value=mock_stream
+ )
+ stream = llm.stream("Hello langchain")
+ full: Optional[BaseMessageChunk] = None
+ for i, chunk in enumerate(stream):
+ full = chunk if full is None else full + chunk
+ assert chunk.content == mock_chunks[i]["choices"][0]["delta"]["content"]
+ if i == 0:
+ assert chunk.additional_kwargs["citations"] == [
+ "example.com",
+ "example2.com",
+ ]
+ assert chunk.additional_kwargs["images"] == [
+ {
+ "image_url": "mock_image_url",
+ "origin_url": "mock_origin_url",
+ "height": 100,
+ "width": 100,
+ }
+ ]
+ else:
+ assert "citations" not in chunk.additional_kwargs
+ assert "images" not in chunk.additional_kwargs
+ assert isinstance(full, AIMessageChunk)
+ assert full.content == "Hello Perplexity"
+ assert full.additional_kwargs == {
+ "citations": ["example.com", "example2.com"],
+ "images": [
+ {
+ "image_url": "mock_image_url",
+ "origin_url": "mock_origin_url",
+ "height": 100,
+ "width": 100,
+ }
+ ],
+ }
+
+ patcher.assert_called_once()
+
+
+def test_perplexity_stream_includes_citations_and_related_questions(
+ mocker: MockerFixture,
+) -> None:
+ """Test that the stream method includes citations in the additional_kwargs."""
+ llm = ChatPerplexity(
+ model="test",
+ timeout=30,
+ verbose=True,
+ )
+ mock_chunk_0 = {
+ "choices": [
+ {
+ "delta": {
+ "content": "Hello ",
+ },
+ "finish_reason": None,
+ }
+ ],
+ "citations": ["example.com", "example2.com"],
+ "related_questions": ["example_question_1", "example_question_2"],
+ }
+ mock_chunk_1 = {
+ "choices": [
+ {
+ "delta": {
+ "content": "Perplexity",
+ },
+ "finish_reason": None,
+ }
+ ],
+ "citations": ["example.com", "example2.com"],
+ "related_questions": ["example_question_1", "example_question_2"],
+ }
+ mock_chunks: List[Dict[str, Any]] = [mock_chunk_0, mock_chunk_1]
+ mock_stream = MagicMock()
+ mock_stream.__iter__.return_value = mock_chunks
+ patcher = mocker.patch.object(
+ llm.client.chat.completions, "create", return_value=mock_stream
+ )
+ stream = llm.stream("Hello langchain")
+ full: Optional[BaseMessageChunk] = None
+ for i, chunk in enumerate(stream):
+ full = chunk if full is None else full + chunk
+ assert chunk.content == mock_chunks[i]["choices"][0]["delta"]["content"]
+ if i == 0:
+ assert chunk.additional_kwargs["citations"] == [
+ "example.com",
+ "example2.com",
+ ]
+ assert chunk.additional_kwargs["related_questions"] == [
+ "example_question_1",
+ "example_question_2",
+ ]
+ else:
+ assert "citations" not in chunk.additional_kwargs
+ assert "related_questions" not in chunk.additional_kwargs
+ assert isinstance(full, AIMessageChunk)
+ assert full.content == "Hello Perplexity"
+ assert full.additional_kwargs == {
+ "citations": ["example.com", "example2.com"],
+ "related_questions": ["example_question_1", "example_question_2"],
+ }
+
+ patcher.assert_called_once()
diff --git a/libs/community/uv.lock b/libs/community/uv.lock
index f5ce918fd7..91ccbf4f44 100644
--- a/libs/community/uv.lock
+++ b/libs/community/uv.lock
@@ -1,4 +1,5 @@
version = 1
+revision = 1
requires-python = ">=3.9, <4.0"
resolution-markers = [
"python_full_version >= '3.12.4' and platform_python_implementation == 'PyPy'",
@@ -1531,6 +1532,7 @@ requires-dist = [
{ name = "requests", specifier = ">=2,<3" },
{ name = "sqlalchemy", specifier = ">=1.4,<3" },
]
+provides-extras = ["community", "anthropic", "openai", "azure-ai", "cohere", "google-vertexai", "google-genai", "fireworks", "ollama", "together", "mistralai", "huggingface", "groq", "aws", "deepseek", "xai"]
[package.metadata.requires-dev]
codespell = [{ name = "codespell", specifier = ">=2.2.0,<3.0.0" }]
@@ -1745,7 +1747,7 @@ typing = [
[[package]]
name = "langchain-core"
-version = "0.3.45"
+version = "0.3.47"
source = { editable = "../core" }
dependencies = [
{ name = "jsonpatch" },
@@ -1803,7 +1805,7 @@ typing = [
[[package]]
name = "langchain-tests"
-version = "0.3.14"
+version = "0.3.15"
source = { editable = "../standard-tests" }
dependencies = [
{ name = "httpx" },