Skip to content

fix(http): honor content-type charset before encoding detection (graphon #93) - #41252

Open
wylswz wants to merge 1 commit into
lts/1.13.xfrom
cherry-pick-graphon93-lts-1.13.x
Open

fix(http): honor content-type charset before encoding detection (graphon #93)#41252
wylswz wants to merge 1 commit into
lts/1.13.xfrom
cherry-pick-graphon93-lts-1.13.x

Conversation

@wylswz

@wylswz wylswz commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of graphon PR #93 ("honor content-type charset before encoding detection") to lts/1.13.x.

On lts/1.13.x the graph engine is still vendored in-repo (api/dify_graph/), so this is applied as a manual port rather than a bumped graphon dependency (which is how main carries the fix).

Root cause: the HTTP request node's Response.text ran charset_normalizer detection first and ignored the charset declared in the response Content-Type header. Auto-detection can misjudge UTF-8 as another encoding, producing garbled Chinese output — even though the server declared the correct charset.

Fix: honor the declared Content-Type charset first (decode with errors="replace"), then fall back to charset_normalizer and finally httpx's built-in detection when no charset is declared. Added _extract_charset_from_content_type() using email.message.Message.get_content_charset() so quoted parameters are handled correctly.

Public record: dify #31695.

Test plan

  • New unit tests in test_entities.py: declared charset takes precedence over detection, GBK real-world case, errors="replace" on invalid bytes, and charset nested in quoted params is ignored.
  • Existing Response.text tests still pass (40 passed).
  • ruff check / ruff format / basedpyright clean on changed files.

Resolves ENG-767

The HTTP request node's Response.text guessed the encoding with
charset_normalizer first and ignored the charset declared in the
Content-Type response header. Auto-detection can misjudge UTF-8 as
another encoding, producing garbled Chinese text. Honor the declared
charset first, then fall back to charset_normalizer and httpx.

Manual backport of graphon PR #93 (the graph engine is vendored in
api/dify_graph on lts/1.13.x, not the external graphon package).
Public record: dify #31695.

(manually backported from graphon PR langgenius/graphon#93)
Resolves ENG-767
@wylswz
wylswz requested a review from a team August 25, 2026 10:01
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Aug 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 0.00% 0.00% 0.00%
Strict coverage 0.00% 0.00% 0.00%
Typed symbols 0 0 0
Untyped symbols 0 0 0
Modules 0 0 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant