Skip to content

fix(binding-files): keep UTF-8 text preview when max_bytes splits a character - #41761

Merged
crazywoola merged 1 commit into
langgenius:mainfrom
Souravrajvi0:feat/fix-binding-file-utf8-preview-41760-af13
Sep 4, 2026
Merged

fix(binding-files): keep UTF-8 text preview when max_bytes splits a character#41761
crazywoola merged 1 commit into
langgenius:mainfrom
Souravrajvi0:feat/fix-binding-file-utf8-preview-41760-af13

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Binding file previews currently decode a hard max_bytes slice 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:

  1. Minimal: note.txt = a€ (61 e2 82 ac) with max_bytes: 2
  2. Default preview: 262143 ASCII bytes followed by with max_bytes: 262144

This 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 character
  • dify-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 file

Test plan

  • pytest tests/local/dify_agent/server/test_binding_files.py (34 passed)
  • Manual POST /execution-bindings/files/read on note.txt (a€) with max_bytes: 2 returned {"path":"note.txt","size":4,"truncated":true,"binary":false,"text":"a"}
  • Manual 4-byte split (a😀, max_bytes: 3) returned {"binary":false,"text":"a","truncated":true}
  • Manual invalid UTF-8 in the middle (a\xff€, max_bytes: 4) still returned {"binary":true,"text":null}

…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
@crazywoola
crazywoola added this pull request to the merge queue Sep 4, 2026
Merged via the queue into langgenius:main with commit df0460f Sep 4, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binding file preview reports valid UTF-8 text as binary when max_bytes splits a character

2 participants