langgenius / langgenius/dify

[Bug]:short media data is duplicated when saving prompts

Open Beginner friendly
#41,671 3 comments 2 reactions 0 assignees View on GitHub
🐞 bug
Dominant language
TypeScript
Stars
156k
Forks
24.6k
Avg merge
22h 9m
Merged PRs (30d)
610

Description

### Self Checks

- [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542).
- [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general).
- [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.

### Dify version

main

### Cloud or Self Hosted

Self Hosted (Source)

### Steps to reproduce

In `api/core/prompt/utils/prompt_message_util.py`, `PromptMessageUtil.prompt_messages_to_prompt_for_saving` builds the image/audio `data` field with:

"data": content.data[:10] + "...[TRUNCATED]..." + content.data[-10:]

For inputs of 20 characters or fewer, the head and tail slices overlap and the content is duplicated.

Reproduction (code-level):

1. Construct a `MultiModalPromptMessageContent` (image or audio) whose resolved `data` is ≤ 20 characters. Because `MultiModalPromptMessageContent.data` resolves to `self.url` when `url` is set, a short URL such as `url="abc"` yields `content.data == "abc"`.
2. Call `PromptMessageUtil.prompt_messages_to_prompt_for_saving` on a prompt containing that content (the bug exists in both the CHAT and COMPLETION branches of the function).
3. Inspect the serialized `data` value in the returned list.

### ✔️ Expected Behavior

Short media data (≤ 20 characters) should be kept verbatim in the saved `data` field. Truncation should only apply when the data is longer than twice the kept length (10 + 10). For the example above (`data == "abc"`), the saved `data` should equal `"abc"`.

### ❌ Actual Behavior

The saved `data` field equals `"abc...[TRUNCATED]...abc"` — the short content is duplicated and the `...[TRUNCATED]...` marker is inserted even though the data is shorter than the kept window. The same duplication happens for any data of length 1–20 (e.g. `"hello"` becomes `"hello...[TRUNCATED]...hello"`), and data of length 11–20 produces a middle slice that overlaps itself.

Contributor guide

Open the contributing guide

Research direction

Start in api/core/prompt/utils/prompt_message_util.py at PromptMessageUtil.prompt_messages_to_prompt_for_saving and inspect the media handling in both the CHAT and COMPLETION branches. Verify behavior with short image or audio data and confirm that truncation applies only beyond the 10-character head and tail window, while shorter data remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.