Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(workflow): restore structured output fallback and boolean types (#44962)

zyssyz1230bd201254da4
diff --git a/.github/workflows/knowledge-fs-ci.yml b/.github/workflows/knowledge-fs-ci.yml
index 2e1740d1c6e..0d5a290f32f 100644
--- a/.github/workflows/knowledge-fs-ci.yml
+++ b/.github/workflows/knowledge-fs-ci.yml
@@ -266,6 +266,7 @@ jobs:
api/tests/unit_tests/core/app/apps/agent_app/test_runtime_request_builder.py
api/tests/unit_tests/core/workflow/nodes/agent_v2/test_runtime_request_builder.py
api/tests/unit_tests/core/workflow/test_node_runtime.py
+ api/tests/unit_tests/core/workflow/test_structured_output_compatibility.py
api/tests/unit_tests/services/enterprise/test_rbac_service.py
api/tests/unit_tests/repositories/workspace/test_workspace_repository.py
api/tests/unit_tests/services/test_agent_tool_inner_service.py
diff --git a/api/core/llm_generator/output_parser/structured_output.py b/api/core/llm_generator/output_parser/structured_output.py
index 8205c184a31..126fd7ffdfc 100644
--- a/api/core/llm_generator/output_parser/structured_output.py
+++ b/api/core/llm_generator/output_parser/structured_output.py
@@ -339,9 +339,6 @@ def _prepare_schema_for_model(provider: str, model_schema: AIModelEntity, schema
# Deep copy to avoid modifying the original schema
processed_schema = dict(deepcopy(schema))
- # Convert boolean types to string types (common requirement)
- convert_boolean_to_string(processed_schema)
-
# Apply model-specific transformations
if SpecialModelType.GEMINI in model_schema.model:
remove_additional_properties(processed_schema)
@@ -375,27 +372,3 @@ def remove_additional_properties(schema: dict[str, Any]) -> None:
for item in value:
if isinstance(item, dict):
remove_additional_properties(item)
-
-
-def convert_boolean_to_string(schema: dict[str, Any]) -> None:
- """
- Convert boolean type specifications to string in JSON schema.
-
- :param schema: JSON schema to modify in-place
- """
- if not isinstance(schema, dict):
- return
-
- # Check for boolean type at current level
- if schema.get("type") == "boolean":
- schema["type"] = "string"
-
- # Process nested dictionaries and lists recursively
- for value in schema.values():
- match value:
- case dict():
- convert_boolean_to_string(value)
- case list():
- for item in value:
- if isinstance(item, dict):
- convert_boolean_to_string(item)
diff --git a/api/core/llm_generator/prompts.py b/api/core/llm_generator/prompts.py
index 08722df595c..62d657f73ac 100644
--- a/api/core/llm_generator/prompts.py
+++ b/api/core/llm_generator/prompts.py
@@ -297,17 +297,18 @@ Now, generate a JSON Schema based on my description
STRUCTURED_OUTPUT_PROMPT = """You’re a helpful AI assistant. You could answer questions and output in JSON format.
constraints:
- You must output in JSON format.
- - Do not output boolean value, use string type instead.
- - Do not output integer or float value, use number type instead.
+ - Strictly follow the provided JSON schema, including all field types.
+ - Use JSON true or false for boolean values, never strings.
+ - Use JSON numbers for numeric fields and JSON null where allowed by the schema.
eg:
Here is the JSON schema:
- {"additionalProperties": false, "properties": {"age": {"type": "number"}, "name": {"type": "string"}}, "required": ["name", "age"], "type": "object"}
+ {"additionalProperties": false, "properties": {"age": {"type": "number"}, "name": {"type": "string"}, "is_active": {"type": "boolean"}}, "required": ["name", "age", "is_active"], "type": "object"}
Here is the user's question:
- My name is John Doe and I am 30 years old.
+ My name is John Doe, I am 30 years old, and my account is active.
output:
- {"name": "John Doe", "age": 30}
+ {"name": "John Doe", "age": 30, "is_active": true}
Here is the JSON schema:
{{schema}}
""" # noqa: E501
@@ -436,7 +437,7 @@ Requirements:
4. If images are included, describe their key information
5. Do not use words like "好的", "ok", "I understand", "This text discusses", "The content mentions"
6. Write directly without extra words
-7. If there is not enough content to generate a meaningful summary,
+7. If there is not enough content to generate a meaningful summary,
return an empty string without any explanation or prompt
Output only the summary text. Start summarizing now:
diff --git a/api/core/workflow/node_runtime.py b/api/core/workflow/node_runtime.py
index 3d25dab5783..cd08dab797c 100644
--- a/api/core/workflow/node_runtime.py
+++ b/api/core/workflow/node_runtime.py
@@ -59,7 +59,7 @@ from graphon.model_runtime.entities.llm_entities import (
LLMUsage,
)
from graphon.model_runtime.entities.message_entities import PromptMessage, PromptMessageTool
-from graphon.model_runtime.entities.model_entities import AIModelEntity
+from graphon.model_runtime.entities.model_entities import AIModelEntity, ModelFeature
from graphon.model_runtime.model_providers.base.large_language_model import LargeLanguageModel
from graphon.nodes.llm.runtime_protocols import (
LLMPollingCapableProtocol,
@@ -215,6 +215,15 @@ class DifyPreparedLLM(LLMProtocol):
@override
def get_model_schema(self) -> AIModelEntity:
+ model_schema = self._get_provider_model_schema()
+ if model_schema.features is None or ModelFeature.STRUCTURED_OUTPUT in model_schema.features:
+ return model_schema
+ # Graphon checks this adapter's capabilities before invocation. Dify can
+ # supply structured output through a schema prompt without native support.
+ # Copy the schema so provider metadata still selects the correct path.
+ return model_schema.model_copy(update={"features": [*model_schema.features, ModelFeature.STRUCTURED_OUTPUT]})
+
+ def _get_provider_model_schema(self) -> AIModelEntity:
model_schema = cast(LargeLanguageModel, self._model_instance.model_type_instance).get_model_schema(
self._model_instance.model_name,
self._model_instance.credentials,
@@ -302,7 +311,7 @@ class DifyPreparedLLM(LLMProtocol):
) -> LLMResultWithStructuredOutput | Generator[LLMResultChunkWithStructuredOutput]:
return invoke_llm_with_structured_output(
provider=self.provider,
- model_schema=self.get_model_schema(),
+ model_schema=self._get_provider_model_schema(),
model_instance=self._model_instance,
prompt_messages=prompt_messages,
json_schema=json_schema,
@@ -323,6 +332,11 @@ class DifyPreparedLLM(LLMProtocol):
class DifyPreparedPollingLLM(DifyPreparedLLM, LLMPollingCapableProtocol):
"""Prepared workflow LLM adapter that exposes Graphon's polling protocol."""
+ @override
+ def get_model_schema(self) -> AIModelEntity:
+ # Polling passes the schema directly to the plugin and has no prompt fallback.
+ return self._get_provider_model_schema()
+
def __init__(self, model_instance: ModelInstance, request_metadata: Mapping[str, object] | None = None) -> None:
super().__init__(model_instance, request_metadata=request_metadata)
model_type_instance = cast(LargeLanguageModel, model_instance.model_type_instance)
diff --git a/api/tests/unit_tests/core/llm_generator/output_parser/test_structured_output.py b/api/tests/unit_tests/core/llm_generator/output_parser/test_structured_output.py
index fd22a8295b7..3b64609ff62 100644
--- a/api/tests/unit_tests/core/llm_generator/output_parser/test_structured_output.py
+++ b/api/tests/unit_tests/core/llm_generator/output_parser/test_structured_output.py
@@ -1,12 +1,14 @@
import inspect
import json
from collections.abc import Iterator
+from copy import deepcopy
from decimal import Decimal
from types import SimpleNamespace
from typing import Literal, NotRequired
from unittest.mock import MagicMock, patch
import pytest
+from jsonschema import Draft7Validator
from typing_extensions import TypedDict
from core.llm_generator.output_parser.errors import OutputParserError
@@ -17,7 +19,6 @@ from core.llm_generator.output_parser.structured_output import (
_parse_structured_output,
_prepare_schema_for_model,
_set_response_format,
- convert_boolean_to_string,
invoke_llm_with_structured_output,
remove_additional_properties,
)
@@ -62,9 +63,11 @@ class _ModelInstance(ModelInstance):
def __init__(self, response: LLMResult | Iterator[LLMResultChunk | SimpleNamespace]) -> None:
self.response = response
+ self.invocations: list[dict[str, object]] = []
def invoke_llm(self, **kwargs: object):
inspect.signature(ModelInstance.invoke_llm).bind(self, **kwargs)
+ self.invocations.append(kwargs)
return self.response
@@ -86,23 +89,125 @@ class TestStructuredOutput:
remove_additional_properties(None) # Should not raise
remove_additional_properties([]) # Should not raise
- def test_convert_boolean_to_string(self):
+ @pytest.mark.parametrize("streamed", [False, True], ids=["blocking", "streaming"])
+ @pytest.mark.parametrize(
+ ("provider", "model", "native"),
+ [
+ ("openai", "gpt-4.1", True),
+ ("google", "gemini-1.5-pro", True),
+ ("ollama", "llama3", True),
+ ("anthropic", "claude-3", False),
+ ],
+ ids=["openai-native", "gemini-native", "ollama-native", "prompt-fallback"],
+ )
+ def test_invoke_preserves_boolean_schema_and_values(
+ self, provider: str, model: str, native: bool, streamed: bool
+ ) -> None:
schema = {
"type": "object",
"properties": {
- "is_active": {"type": "boolean"},
+ "is_active": {"type": "boolean", "enum": [True, False], "default": False},
+ "nested": {
+ "type": "object",
+ "properties": {"visible": {"type": "boolean"}},
+ "required": ["visible"],
+ "additionalProperties": False,
+ },
"tags": {"type": "array", "items": {"type": "boolean"}},
- "list_schema": [{"type": "boolean"}],
+ "optional": {"anyOf": [{"type": "boolean"}, {"type": "null"}]},
+ "nullable": {"type": ["boolean", "null"]},
},
+ "required": ["is_active", "nested", "tags", "optional", "nullable"],
+ "additionalProperties": False,
+ }
+ original_schema = deepcopy(schema)
+ response = {
+ "is_active": False,
+ "nested": {"visible": True},
+ "tags": [True, False],
+ "optional": None,
+ "nullable": False,
}
- convert_boolean_to_string(schema)
- assert schema["properties"]["is_active"]["type"] == "string"
- assert schema["properties"]["tags"]["items"]["type"] == "string"
- assert schema["properties"]["list_schema"][0]["type"] == "string"
+ model_schema = make_model_schema(model)
+ model_schema.features = [ModelFeature.STRUCTURED_OUTPUT] if native else []
+ model_schema.parameter_rules = [
+ ParameterRule(
+ name="response_format",
+ label=I18nObject(en_US="Response format"),
+ type=ParameterType.STRING,
+ options=[ResponseFormat.JSON_SCHEMA, ResponseFormat.JSON_OBJECT],
+ )
+ ]
+ text = json.dumps(response)
+ if streamed:
+ llm_response = iter(
+ [
+ LLMResultChunk(
+ model=model,
+ delta=LLMResultChunkDelta(index=index, message=AssistantPromptMessage(content=part)),
+ )
+ for index, part in enumerate((text[: len(text) // 2], text[len(text) // 2 :]))
+ ]
+ )
+ else:
+ llm_response = LLMResult(
+ model=model,
+ message=AssistantPromptMessage(content=text),
+ usage=LLMUsage.empty_usage(),
+ )
+ model_instance = _ModelInstance(llm_response)
- # Test with non-dict input
- convert_boolean_to_string(None) # Should not raise
- convert_boolean_to_string([]) # Should not raise
+ result = invoke_llm_with_structured_output(
+ provider=provider,
+ model_schema=model_schema,
+ model_instance=model_instance,
+ prompt_messages=[UserPromptMessage(content="Return the account state")],
+ json_schema=schema,
+ stream=streamed,
+ )
+
+ output = (
+ result.structured_output
+ if isinstance(result, LLMResultWithStructuredOutput)
+ else list(result)[-1].structured_output
+ )
+ assert output is not None
+ assert output == response
+ assert output["is_active"] is False
+ Draft7Validator(schema).validate(output)
+ assert schema == original_schema
+ assert len(model_instance.invocations) == 1
+ invocation = model_instance.invocations[0]
+ parameters = invocation["model_parameters"]
+ assert isinstance(parameters, dict)
+ if native:
+ prepared = json.loads(parameters["json_schema"])
+ expected_schema = deepcopy(schema)
+ if provider == "google":
+ expected_schema.pop("additionalProperties")
+ expected_schema["properties"]["nested"].pop("additionalProperties")
+ elif provider == "openai":
+ assert prepared["name"] == "llm_response"
+ prepared = prepared["schema"]
+ assert prepared == expected_schema
+ assert parameters["response_format"] == ResponseFormat.JSON_SCHEMA
+ Draft7Validator(prepared).validate(output)
+ else:
+ assert "json_schema" not in parameters
+ assert parameters["response_format"] == ResponseFormat.JSON_OBJECT
+ prompts = invocation["prompt_messages"]
+ assert isinstance(prompts, list)
+ assert isinstance(prompts[0], SystemPromptMessage)
+ assert isinstance(prompts[0].content, str)
+ assert json.dumps(schema) in prompts[0].content
+ assert "Use JSON true or false for boolean values, never strings." in prompts[0].content
+ assert "Do not output boolean value" not in prompts[0].content
+
+ @pytest.mark.parametrize("value", [False, True, None, "false", "true"])
+ def test_parse_structured_output_preserves_value_types(self, value: bool | str | None) -> None:
+ result = _parse_structured_output(json.dumps({"value": value}))
+ assert result["value"] == value
+ assert type(result["value"]) is type(value)
def test_parse_structured_output_valid(self):
text = '{"key": "value"}'
@@ -972,7 +1077,7 @@ def test_model_specific_schema_preparation() -> None:
model_instance = get_model_instance()
model_instance.invoke_llm.return_value = LLMResult(
model="gemini-pro",
- message=AssistantPromptMessage(content='{"result": "true"}'),
+ message=AssistantPromptMessage(content='{"result": true}'),
usage=create_mock_usage(prompt_tokens=10, completion_tokens=5),
)
@@ -993,5 +1098,10 @@ def test_model_specific_schema_preparation() -> None:
model_instance.invoke_llm.assert_called_once()
call_args = model_instance.invoke_llm.call_args
- # For Gemini, the schema should not have additionalProperties and boolean should be converted to string
+ # Gemini omits additionalProperties while retaining the requested boolean type.
assert "json_schema" in call_args.kwargs["model_parameters"]
+ prepared_schema = json.loads(call_args.kwargs["model_parameters"]["json_schema"])
+ assert "additionalProperties" not in prepared_schema
+ assert prepared_schema["properties"]["result"]["type"] == "boolean"
+ assert result.structured_output is not None
+ assert result.structured_output["result"] is True
diff --git a/api/tests/unit_tests/core/workflow/test_structured_output_compatibility.py b/api/tests/unit_tests/core/workflow/test_structured_output_compatibility.py
new file mode 100644
index 00000000000..af1778f7773
--- /dev/null
+++ b/api/tests/unit_tests/core/workflow/test_structured_output_compatibility.py
@@ -0,0 +1,273 @@
+"""Exercise provider capability compatibility through the real workflow LLM adapter."""
+
+from __future__ import annotations
+
+import json
+import time
+from collections.abc import Generator, Mapping, Sequence
+from dataclasses import dataclass
+
+import pytest
+
+from core.helper.ssrf_proxy import graphon_ssrf_proxy
+from core.model_manager import ModelInstance
+from core.workflow.node_runtime import (
+ DifyPreparedLLM,
+ DifyPreparedPollingLLM,
+ DifyPromptMessageSerializer,
+ build_dify_llm_file_saver,
+ resolve_dify_run_context,
+)
+from graphon.enums import WorkflowNodeExecutionStatus
+from graphon.model_runtime.entities.common_entities import I18nObject
+from graphon.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta, LLMUsage
+from graphon.model_runtime.entities.message_entities import AssistantPromptMessage, PromptMessage, SystemPromptMessage
+from graphon.model_runtime.entities.model_entities import AIModelEntity, ModelFeature, ParameterRule, ParameterType
+from graphon.node_events import StreamCompletedEvent
+from graphon.nodes.llm.entities import LLMNodeData
+from graphon.nodes.llm.node import LLMNode
+from graphon.runtime import RuntimeState, VariablePool
+from tests.helpers.unit.core.model_fixtures import make_model_config, make_model_instance
+from tests.workflow_test_utils import build_test_graph_init_params
+
+_PROVIDER = "openai"
+_MODEL = "gpt-4o-mini"
+_OUTPUT_SCHEMA = {
+ "type": "object",
+ "properties": {
+ "profile": {
+ "type": "object",
+ "properties": {"active": {"type": "boolean"}},
+ "required": ["active"],
+ },
+ "flags": {"type": "array", "items": {"type": "boolean"}},
+ },
+ "required": ["profile", "flags"],
+}
+_VALID_OUTPUT = {"profile": {"active": False}, "flags": [False, True]}
+_FEATURE_CASES = [
+ pytest.param(None, id="unknown"),
+ pytest.param([], id="empty"),
+ pytest.param([ModelFeature.TOOL_CALL], id="tool-call-only"),
+ pytest.param([ModelFeature.STRUCTURED_OUTPUT], id="native-structured-output"),
+]
+
+
+@dataclass(frozen=True)
+class _ProviderCall:
+ prompt_messages: Sequence[PromptMessage]
+ model_parameters: dict[str, object]
+
+
+def _provider_model(
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ features: list[ModelFeature] | None,
+ response: Mapping[str, object],
+ streamed: bool,
+) -> tuple[ModelInstance, AIModelEntity, list[_ProviderCall]]:
+ """Replace provider schema discovery and responses, retaining the Dify/parser path."""
+ model_instance = make_model_instance(provider=_PROVIDER, model=_MODEL)
+ schema = make_model_config(provider=_PROVIDER, model=_MODEL, mode="chat").model_schema.model_copy(
+ update={
+ "features": features,
+ "parameter_rules": [
+ ParameterRule(
+ name="response_format",
+ label=I18nObject(en_US="Response format"),
+ type=ParameterType.STRING,
+ options=["json_schema", "json_object"],
+ )
+ ],
+ }
+ )
+ calls: list[_ProviderCall] = []
+ text = json.dumps(response)
+
+ def get_model_schema(*_args: object, **_kwargs: object) -> AIModelEntity:
+ return schema
+
+ def invoke_model(
+ *,
+ prompt_messages: Sequence[PromptMessage],
+ model_parameters: dict[str, object],
+ stream: bool,
+ **_kwargs: object,
+ ) -> LLMResult | Generator[LLMResultChunk]:
+ calls.append(_ProviderCall(prompt_messages=prompt_messages, model_parameters=model_parameters))
+ assert stream is True
+ usage = LLMUsage.empty_usage()
+ if not streamed:
+ return LLMResult(
+ model=_MODEL,
+ prompt_messages=prompt_messages,
+ message=AssistantPromptMessage(content=text),
+ usage=usage,
+ )
+ return (
+ LLMResultChunk(
+ model=_MODEL,
+ prompt_messages=prompt_messages,
+ delta=LLMResultChunkDelta(
+ index=index,
+ message=AssistantPromptMessage(content=part),
+ usage=usage if index == 1 else None,
+ finish_reason="stop" if index == 1 else None,
+ ),
+ )
+ for index, part in enumerate((text[: len(text) // 2], text[len(text) // 2 :]))
+ )
+
+ monkeypatch.setattr(model_instance.model_type_instance, "get_model_schema", get_model_schema)
+ monkeypatch.setattr(model_instance, "invoke_llm", invoke_model)
+ return model_instance, schema, calls
+
+
+def _structured_node(prepared: DifyPreparedLLM) -> LLMNode:
+ init_params = build_test_graph_init_params()
+ run_context = resolve_dify_run_context(init_params.run_context)
+ return LLMNode(
+ node_id="llm",
+ data=LLMNodeData.model_validate(
+ {
+ "title": "Boolean output",
+ "type": "llm",
+ "model": {"provider": _PROVIDER, "name": _MODEL, "mode": "chat"},
+ "prompt_template": [{"role": "user", "text": "Return the requested boolean fields."}],
+ "context": {"enabled": False},
+ "structured_output_switch_on": True,
+ "structured_output": {"schema": _OUTPUT_SCHEMA},
+ }
+ ),
+ init_params=init_params,
+ runtime_state=RuntimeState(
+ workflow_id=init_params.workflow_id, variable_pool=VariablePool(), start_at=time.perf_counter()
+ ),
+ model_instance=prepared,
+ llm_file_saver=build_dify_llm_file_saver(run_context=run_context, http_client=graphon_ssrf_proxy),
+ prompt_message_serializer=DifyPromptMessageSerializer(),
+ )
+
+
+def _assert_schema_route(call: _ProviderCall, *, native: bool) -> None:
+ parameters = call.model_parameters
+ system_prompts = [message for message in call.prompt_messages if isinstance(message, SystemPromptMessage)]
+ if native:
+ schema_parameter = parameters["json_schema"]
+ assert isinstance(schema_parameter, str)
+ assert json.loads(schema_parameter) == {"name": "llm_response", "schema": _OUTPUT_SCHEMA}
+ assert parameters["response_format"] == "json_schema"
+ assert not system_prompts
+ else:
+ assert "json_schema" not in parameters
+ assert parameters["response_format"] == "json_object"
+ assert len(system_prompts) == 1
+ assert isinstance(system_prompts[0].content, str)
+ assert json.dumps(_OUTPUT_SCHEMA, ensure_ascii=False) in system_prompts[0].content
+
+
[email protected]("features", _FEATURE_CASES)
[email protected]("streamed", [False, True], ids=["blocking", "streaming"])
+def test_structured_output_reaches_provider_and_preserves_nested_booleans(
+ monkeypatch: pytest.MonkeyPatch, features: list[ModelFeature] | None, streamed: bool
+) -> None:
+ model_instance, provider_schema, calls = _provider_model(
+ monkeypatch, features=features, response=_VALID_OUTPUT, streamed=streamed
+ )
+ original_schema = provider_schema.model_dump(mode="json")
+ node = _structured_node(DifyPreparedLLM(model_instance))
+
+ completions = [event.node_run_result for event in node._run() if isinstance(event, StreamCompletedEvent)]
+
+ assert len(completions) == 1
+ result = completions[0]
+ assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED, result.error
+ assert result.outputs is not None
+ output = result.outputs["structured_output"]
+ assert output == _VALID_OUTPUT
+ assert output["profile"]["active"] is False
+ assert output["flags"][0] is False
+ assert output["flags"][1] is True
+ assert len(calls) == 1
+ _assert_schema_route(calls[0], native=bool(features and ModelFeature.STRUCTURED_OUTPUT in features))
+ assert provider_schema.model_dump(mode="json") == original_schema
+
+
+ ("response", "error_path"),
+ [
+ pytest.param({"profile": {"active": "false"}, "flags": [False]}, "$.profile.active", id="nested"),
+ pytest.param({"profile": {"active": False}, "flags": ["false"]}, "$.flags[0]", id="array"),
+ ],
+)
+ "features",
+ [pytest.param([ModelFeature.TOOL_CALL], id="prompt"), pytest.param([ModelFeature.STRUCTURED_OUTPUT], id="native")],
+)
[email protected]("streamed", [False, True], ids=["blocking", "streaming"])
+def test_string_boolean_still_fails_strict_result_validation(
+ monkeypatch: pytest.MonkeyPatch,
+ response: dict[str, object],
+ error_path: str,
+ features: list[ModelFeature],
+ streamed: bool,
+) -> None:
+ model_instance, _, calls = _provider_model(monkeypatch, features=features, response=response, streamed=streamed)
+ node = _structured_node(DifyPreparedLLM(model_instance))
+
+ completions = [event.node_run_result for event in node._run() if isinstance(event, StreamCompletedEvent)]
+
+ assert len(completions) == 1
+ result = completions[0]
+ assert result.status == WorkflowNodeExecutionStatus.FAILED
+ assert result.error_type == "LLMNodeError"
+ assert result.error is not None
+ assert f"stage=result, path={error_path}" in result.error
+ assert "is not of type 'boolean'" in result.error
+ assert not result.outputs
+ assert len(calls) == 1
+ _assert_schema_route(calls[0], native=ModelFeature.STRUCTURED_OUTPUT in features)
+
+
[email protected]("features", _FEATURE_CASES)
+def test_effective_capabilities_do_not_mutate_provider_schema_or_accumulate(
+ monkeypatch: pytest.MonkeyPatch, features: list[ModelFeature] | None
+) -> None:
+ model_instance, provider_schema, _ = _provider_model(
+ monkeypatch, features=features, response=_VALID_OUTPUT, streamed=False
+ )
+ original_schema = provider_schema.model_dump(mode="json")
+ prepared = DifyPreparedLLM(model_instance)
+
+ for _ in range(3):
+ effective_schema = prepared.get_model_schema()
+ if features is None:
+ assert effective_schema.features is None
+ else:
+ assert effective_schema.features is not None
+ assert effective_schema.features.count(ModelFeature.STRUCTURED_OUTPUT) == 1
+ assert all(feature in effective_schema.features for feature in features)
+ assert provider_schema.model_dump(mode="json") == original_schema
+
+
[email protected]("native", [False, True], ids=["without-native-schema", "with-native-schema"])
+def test_polling_adapter_preserves_provider_capabilities(monkeypatch: pytest.MonkeyPatch, native: bool) -> None:
+ features = [ModelFeature.POLLING]
+ if native:
+ features.append(ModelFeature.STRUCTURED_OUTPUT)
+ model_instance, provider_schema, calls = _provider_model(
+ monkeypatch, features=features, response=_VALID_OUTPUT, streamed=False
+ )
+ prepared = DifyPreparedPollingLLM(model_instance)
+
+ assert prepared.get_model_schema() is provider_schema
+ assert prepared.get_model_schema().features == features
+ assert prepared.get_model_schema().support_structure_output is native
+ assert prepared.get_model_schema().support_polling is True
+ if not native:
+ node = _structured_node(prepared)
+ completions = [event.node_run_result for event in node._run() if isinstance(event, StreamCompletedEvent)]
+ assert len(completions) == 1
+ assert completions[0].status == WorkflowNodeExecutionStatus.FAILED
+ assert "stage=capability" in (completions[0].error or "")
+ assert not calls