Skip to content

fix(dify): support Workflow response format in DifyTranslator#1128

Open
octo-patch wants to merge 2 commits into
PDFMathTranslate:mainfrom
octo-patch:fix/dify-workflow-response-format
Open

fix(dify): support Workflow response format in DifyTranslator#1128
octo-patch wants to merge 2 commits into
PDFMathTranslate:mainfrom
octo-patch:fix/dify-workflow-response-format

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #895

Problem

Dify has three application types with different API response formats:

  • Workflow apps: {"data": {"outputs": {"answer": "..."}}}
  • Agent/Chatflow apps: {"answer": "..."}

The previous DifyTranslator.do_translate() only read the top-level "answer" field, which worked for Agent/Chatflow apps but returned an empty string (no translation) for Workflow apps.

Additionally, the __init__ signatures of DifyTranslator and AnythingLLMTranslator had their lang_in/lang_out parameter names swapped relative to every other translator class. The behaviour was accidentally correct (both the naming and the super().__init__() call were reversed, cancelling each other out), but the confusing naming is a maintenance hazard.

Solution

  • DifyTranslator.do_translate(): Try the Workflow response path (data.outputs.answer) first; fall back to the Agent/Chatflow path (answer) if not present. This makes both app types work transparently with the same translator.
  • Parameter naming: Corrected DifyTranslator.__init__ and AnythingLLMTranslator.__init__ to use the standard lang_in, lang_out order, matching all other translator classes.

Testing

Manually traced the response parsing for both Workflow and Agent/Chatflow response shapes. No existing tests were affected (the parameter reordering is a pure rename with identical runtime behaviour).

octo-patch and others added 2 commits April 23, 2026 09:51
When PyMuPDF's subset_fonts() raises a ValueError (e.g. due to a bad
font width table value in some PDFs), the translation would crash after
completing all the actual translation work. Wrap each subset_fonts call
in a try-except so the output PDF is still written even if font
subsetting fails. The output will be slightly larger in this case.

Fixes PDFMathTranslate#975
Dify has three application types with different response formats:
- Workflow: {"data": {"outputs": {"answer": "..."}}}
- Agent/Chatflow: {"answer": "..."}

The previous code only read the top-level "answer" field, which
returned an empty string for Workflow apps (fixes PDFMathTranslate#895).

This change also corrects the swapped lang_in/lang_out parameter names
in DifyTranslator and AnythingLLMTranslator __init__ signatures. The
previous naming was confusing (the first positional arg was named
lang_out) even though the behaviour was correct due to a double reversal.

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
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.

dify 的格式问题

1 participant