[PR #1279] [MERGED] fix(openai_api_compatible): Ensure JSON schema compatibility #1813

Closed
opened 2026-02-16 10:24:03 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langgenius/dify-official-plugins/pull/1279
Author: @xuzijie1995
Created: 7/14/2025
Status: Merged
Merged: 7/14/2025
Merged by: @crazywoola

Base: mainHead: fix/openai-structured-output


📝 Commits (1)

  • da10e93 fix(openai_api_compatible): Ensure JSON schema compatibility

📊 Changes

2 files changed (+26 additions, -3 deletions)

View changed files

📝 models/openai_api_compatible/manifest.yaml (+1 -1)
📝 models/openai_api_compatible/models/llm/llm.py (+25 -2)

📄 Description

Title: fix(openai_api_compatible): Ensure structured output for 'json_schema' mode

Problem:
OpenAI-compatible models (e.g., GPT-4o) were failing to produce correct structured output when Dify's response_format: 'json_schema' mode was used. The root cause was twofold:

  1. The plugin's base class (OAICompatLargeLanguageModel) does not natively support the 'json_schema' parameter and only recognizes 'json_object'.
  2. There was no mechanism to inject the specific schema rules into the system prompt, leaving the model without explicit instructions on what structure to follow.

Solution:
This commit introduces a compatibility adapter within the _invoke method to intercept and translate these requests correctly.

  • It translates the response_format from the internal 'json_schema' to the standard 'json_object', enabling the model's JSON mode.
  • It injects the full JSON schema from the json_schema parameter directly into the system prompt, providing explicit, actionable instructions to the model.
  • Crucially, the original json_schema parameter is preserved (not popped) to ensure forward compatibility with other potential models that might have their own way of handling it.

Result:
This fix ensures reliable structured output for all OpenAI-compatible models using this feature. It is fully backward-compatible, as the logic only activates when 'json_schema' is explicitly requested, leaving all other operations unaffected.

Related Issues or Context

This change is needed to correctly support Dify's native structured output feature (response_format: 'json_schema') with modern OpenAI-compatible models like GPT-4o, which were previously failing to adhere to the provided schema.

Dify Issue:#21767

This PR contains Changes to Non-LLM Models Plugin

  • []I have Run Comprehensive Tests Relevant to My Changes
image image

This PR contains Changes to LLM Models Plugin

  • My Changes Affect Message Flow Handling (System Messages and User→Assistant Turn-Taking)
  • My Changes Affect Structured Output Format (JSON, XML, etc.)
  • My Changes Affect Token Consumption Metrics

    Note: Injecting the schema into the prompt slightly increases input token consumption, which is an expected and necessary trade-off for ensuring correct output.

  • My Changes Affect Tool Interaction Flow (Multi-Round Usage and Output Handling, for both Agent App and Agent Node)
  • My Changes Affect Multimodal Input Handling (Images, PDFs, Audio, Video, etc.)
  • My Changes Affect Multimodal Output Generation (Images, Audio, Video, etc.)
  • My Changes Affect Other LLM Functionalities (Reasoning Process, Grounding, Prompt Caching, etc.)
  • Other Changes (Add New Models, Fix Model Parameters etc.)

Version Control (Any Changes to the Plugin Will Require Bumping the Version)

  • I have Bumped Up the Version in Manifest.yaml (Top-Level Version Field, Not in Meta Section)

    提醒: 请在提交前,务必将 manifest.yaml 中的版本号升级 (例如,从 0.0.160.0.17)。

Dify Plugin SDK Version

  • I have Ensured dify_plugin>=0.3.0,<0.5.0 is in requirements.txt (SDK docs)

Environment Verification (If Any Code Changes)

Local Deployment Environment

  • Dify Version is: 1.6.0 I have Tested My Changes on Local Deployment Dify with a Clean Environment That Matches the Production Configuration.

SaaS Environment

  • I have Tested My Changes on cloud.dify.ai with a Clean Environment That Matches the Production Configuration

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langgenius/dify-official-plugins/pull/1279 **Author:** [@xuzijie1995](https://github.com/xuzijie1995) **Created:** 7/14/2025 **Status:** ✅ Merged **Merged:** 7/14/2025 **Merged by:** [@crazywoola](https://github.com/crazywoola) **Base:** `main` ← **Head:** `fix/openai-structured-output` --- ### 📝 Commits (1) - [`da10e93`](https://github.com/langgenius/dify-official-plugins/commit/da10e93db48c893a89515b15af32b689e4bbe470) fix(openai_api_compatible): Ensure JSON schema compatibility ### 📊 Changes **2 files changed** (+26 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `models/openai_api_compatible/manifest.yaml` (+1 -1) 📝 `models/openai_api_compatible/models/llm/llm.py` (+25 -2) </details> ### 📄 Description #### **Title:** `fix(openai_api_compatible): Ensure structured output for 'json_schema' mode` **Problem**: OpenAI-compatible models (e.g., GPT-4o) were failing to produce correct structured output when Dify's `response_format: 'json_schema'` mode was used. The root cause was twofold: 1. The plugin's base class (`OAICompatLargeLanguageModel`) does not natively support the `'json_schema'` parameter and only recognizes `'json_object'`. 2. There was no mechanism to inject the specific schema rules into the system prompt, leaving the model without explicit instructions on what structure to follow. **Solution**: This commit introduces a compatibility adapter within the `_invoke` method to intercept and translate these requests correctly. - It translates the `response_format` from the internal `'json_schema'` to the standard `'json_object'`, enabling the model's JSON mode. - It injects the full JSON schema from the `json_schema` parameter directly into the system prompt, providing explicit, actionable instructions to the model. - Crucially, the original `json_schema` parameter is preserved (not popped) to ensure forward compatibility with other potential models that might have their own way of handling it. **Result**: This fix ensures reliable structured output for all OpenAI-compatible models using this feature. It is fully backward-compatible, as the logic only activates when `'json_schema'` is explicitly requested, leaving all other operations unaffected. ## Related Issues or Context This change is needed to correctly support Dify's native structured output feature (`response_format: 'json_schema'`) with modern OpenAI-compatible models like GPT-4o, which were previously failing to adhere to the provided schema. Dify Issue:[#21767](https://github.com/langgenius/dify/issues/21767) ## This PR contains Changes to *Non-LLM Models Plugin* - []I have Run Comprehensive Tests Relevant to My Changes <img width="1030" height="769" alt="image" src="https://github.com/user-attachments/assets/e3308e35-1997-41fa-a73c-b635e9f35e02" /> <img width="1067" height="577" alt="image" src="https://github.com/user-attachments/assets/8cfe42ee-cbe3-488d-b81c-b4940918b82e" /> ## This PR contains Changes to *LLM Models Plugin* - [x] My Changes Affect Message Flow Handling (System Messages and User→Assistant Turn-Taking) - [x] My Changes Affect Structured Output Format (JSON, XML, etc.) - [x] My Changes Affect Token Consumption Metrics > **Note:** Injecting the schema into the prompt slightly increases input token consumption, which is an expected and necessary trade-off for ensuring correct output. - [ ] My Changes Affect Tool Interaction Flow (Multi-Round Usage and Output Handling, for both Agent App and Agent Node) - [ ] My Changes Affect Multimodal Input Handling (Images, PDFs, Audio, Video, etc.) - [ ] My Changes Affect Multimodal Output Generation (Images, Audio, Video, etc.) - [ ] My Changes Affect Other LLM Functionalities (Reasoning Process, Grounding, Prompt Caching, etc.) - [ ] Other Changes (Add New Models, Fix Model Parameters etc.) ## Version Control (Any Changes to the Plugin Will Require Bumping the Version) - [x] I have Bumped Up the Version in Manifest.yaml (Top-Level `Version` Field, Not in Meta Section) > **提醒:** 请在提交前,务必将 `manifest.yaml` 中的版本号升级 (例如,从 `0.0.16` 到 `0.0.17`)。 ## Dify Plugin SDK Version - [x] I have Ensured `dify_plugin>=0.3.0,<0.5.0` is in requirements.txt ([SDK docs](https://github.com/langgenius/dify-plugin-sdks/blob/main/python/README.md)) ## Environment Verification (If Any Code Changes) ### Local Deployment Environment - [x] Dify Version is: `1.6.0` I have Tested My Changes on Local Deployment Dify with a Clean Environment That Matches the Production Configuration. ### SaaS Environment - [ ] I have Tested My Changes on cloud.dify.ai with a Clean Environment That Matches the Production Configuration --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 10:24:03 -05:00
yindo closed this issue 2026-02-16 10:24:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#1813