Files
dify-plugin-sdks/python/tests/test_llm_result.py
Yeuoly 12ad9db9b5 enhancement: remove prompt_messages from LLMResult and LLMResultChunk (#88)
* refactor: remove prompt_messages from LLMResult and LLMResultChunk

* fix: add backward compatibility for deprecated prompt_messages field in LLMResult and LLMResultChunk

* fix: set default value for prompt_messages field to an empty list in LLMResult and LLMResultChunk for backward compatibility

* feat: add test for LLMResult to validate prompt_messages field and ensure backward compatibility

* apply ruff
2025-04-11 15:23:05 +08:00

44 lines
1.6 KiB
Python

from dify_plugin.entities.model.llm import LLMResult, LLMResultChunk, LLMResultChunkDelta, LLMUsage
from dify_plugin.entities.model.message import AssistantPromptMessage, TextPromptMessageContent
def test_build_llm_result_chunk_with_prompt_messages():
chunk = LLMResultChunk(
model="test",
prompt_messages=[AssistantPromptMessage(content=[TextPromptMessageContent(data="Hello, World!")])],
delta=LLMResultChunkDelta(
index=0,
message=AssistantPromptMessage(content=[TextPromptMessageContent(data="Hello, World!")]),
),
)
assert isinstance(chunk.prompt_messages, list)
"""
NOTE:
- https://github.com/langgenius/dify/issues/17799
- https://github.com/langgenius/dify-official-plugins/issues/648
The `prompt_messages` field is deprecated, but to keep backward compatibility
we need to always set it to an empty list.
"""
assert len(chunk.prompt_messages) == 0
def test_build_llm_result_with_prompt_messages():
result = LLMResult(
model="test",
prompt_messages=[AssistantPromptMessage(content=[TextPromptMessageContent(data="Hello, World!")])],
message=AssistantPromptMessage(content=[TextPromptMessageContent(data="Hello, World!")]),
usage=LLMUsage.empty_usage(),
)
assert isinstance(result.prompt_messages, list)
"""
NOTE:
- https://github.com/langgenius/dify/issues/17799
- https://github.com/langgenius/dify-official-plugins/issues/648
The `prompt_messages` field is deprecated, but to keep backward compatibility
we need to always set it to an empty list.
"""
assert len(result.prompt_messages) == 0