fix(binding-files): keep UTF-8 text preview when max_bytes splits a character - #41761
Merged
crazywoola merged 1 commit intoSep 4, 2026
Conversation
…haracter When binding file read truncates at a multi-byte UTF-8 boundary, discard only the incomplete trailing sequence instead of classifying the whole file as binary. Fixes langgenius#41760
Closed
6 tasks
crazywoola
approved these changes
Sep 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Binding file previews currently decode a hard
max_bytesslice with strict UTF-8. When that slice cuts through a valid multi-byte character,decode("utf-8")fails and the whole file is reported as binary.This is one defect with two reproductions from #41760. Both are fixed by this PR:
note.txt=a€(61 e2 82 ac) withmax_bytes: 2€withmax_bytes: 262144This change keeps the longest complete UTF-8 prefix when the preview is truncated at an incomplete trailing sequence (
UnicodeDecodeError.reason == "unexpected end of data"at the end of the buffer). Files with invalid UTF-8 elsewhere are still classified as binary.Fixes #41760
Changes
dify-agent/src/dify_agent/server/binding_files.py: recover truncated text previews that split a 2/3/4-byte UTF-8 characterdify-agent/tests/local/dify_agent/server/test_binding_files.py: cover 2-, 3-, and 4-byte splits plus an invalid byte in the middle of the fileTest plan
pytest tests/local/dify_agent/server/test_binding_files.py(34 passed)POST /execution-bindings/files/readonnote.txt(a€) withmax_bytes: 2returned{"path":"note.txt","size":4,"truncated":true,"binary":false,"text":"a"}a😀,max_bytes: 3) returned{"binary":false,"text":"a","truncated":true}a\xff€,max_bytes: 4) still returned{"binary":true,"text":null}