[PR #5169] fix: JSON mode with an image doesn't work for Gemini #24804

Closed
opened 2026-02-21 20:23:42 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/5169

State: closed
Merged: Yes


Description

Fixes #5031

The problematic code adds the string "```JSON" at the end of the content when applying JSON format, but it doesn't respect the type. The content type is either str | list[PromptMessageContent] but it can add the string to the list[PromptMessageContent]. As a result, it will cause an error when the unexpected type is processed in _convert_one_message_to_txt() in google/llm.py

Example of prompt_messages value (see the value at the end of the content prop)

[SystemPromptMessage(role=<PromptMessageRole.SYSTEM: 'system'>, content='You should always follow the instructions and output a valid JSON object.\nThe structure of the JSON object you can found in the instructions, use {"answer": "$your_answer"} as the default structure\nif you are not sure about the structure.\n\n<instructions>\nPlease output a valid JSON object.\n</instructions>\n', name=None), UserPromptMessage(role=<PromptMessageRole.USER: 'user'>, content=[TextPromptMessageContent(type=<PromptMessageContentType.TEXT: 'text'>, data='What is in the picture?'), ImagePromptMessageContent(type=<PromptMessageContentType.IMAGE: 'image'>, data='https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png', detail=<DETAIL.LOW: 'low'>), '\n', '`', '`', '`', 'J', 'S', 'O', 'N', '\n'], name=None)]

To be honest, I don’t understand the intention of adding ```JSON, but I have modified it to be added at the end of the user prompt text.​ I confirmed that this patch solved the issue.

CleanShot 2024-06-14 at 00 18 56

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • TODO

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/5169 **State:** closed **Merged:** Yes --- # Description Fixes #5031 The problematic code adds the string "```JSON" at the end of the `content` when applying JSON format, but it doesn't respect the type. [The `content` type is either `str | list[PromptMessageContent]`](https://github.com/langgenius/dify/blob/e8afc416dd18b94b09f7ec6749abbcd6f23ad4ec/api/core/model_runtime/entities/message_entities.py#L88) but it can add the string to the `list[PromptMessageContent]`. As a result, it will cause an error when the unexpected type is processed in _convert_one_message_to_txt() in google/llm.py Example of prompt_messages value (see the value at the end of the content prop) ``` [SystemPromptMessage(role=<PromptMessageRole.SYSTEM: 'system'>, content='You should always follow the instructions and output a valid JSON object.\nThe structure of the JSON object you can found in the instructions, use {"answer": "$your_answer"} as the default structure\nif you are not sure about the structure.\n\n<instructions>\nPlease output a valid JSON object.\n</instructions>\n', name=None), UserPromptMessage(role=<PromptMessageRole.USER: 'user'>, content=[TextPromptMessageContent(type=<PromptMessageContentType.TEXT: 'text'>, data='What is in the picture?'), ImagePromptMessageContent(type=<PromptMessageContentType.IMAGE: 'image'>, data='https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png', detail=<DETAIL.LOW: 'low'>), '\n', '`', '`', '`', 'J', 'S', 'O', 'N', '\n'], name=None)] ``` To be honest, I don’t understand the intention of adding ```JSON, but I have modified it to be added at the end of the user prompt text.​ I confirmed that this patch solved the issue. ![CleanShot 2024-06-14 at 00 18 56](https://github.com/langgenius/dify/assets/8503062/47c2d7a7-ac68-4c7a-821b-a6e6a2431ca9) ## Type of Change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [ ] Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement - [ ] Dependency upgrade # How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration - [ ] TODO # Suggested Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:23:42 -05:00
yindo closed this issue 2026-02-21 20:23:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#24804