From b44af19e8d073f54accb576ae4e1ae061ddc4c43 Mon Sep 17 00:00:00 2001 From: pyss56 Date: Mon, 9 Nov 2026 15:06:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(agent):=20=E7=A9=BA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=81=94=E5=90=88=E5=8F=82=E6=95=B0=E5=BD=92=E4=B8=80?= =?UTF-8?q?=E4=B8=BA=20null=20(#7339)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/agent/api/arguments.py | 5 +++++ tests/test_agent_api_arguments.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/test_agent_api_arguments.py diff --git a/app/agent/api/arguments.py b/app/agent/api/arguments.py index 94ca3d7a8..5e8e98c4e 100644 --- a/app/agent/api/arguments.py +++ b/app/agent/api/arguments.py @@ -236,6 +236,11 @@ def _normalize_union(value: Any, schema: dict[str, Any], definitions: dict[str, exact = [item for item in alternatives if item.get("type") == _value_type(value)] if len(exact) == 1: return _normalize_value(value, exact[0], definitions, depth + 1) + # 空字符串表示模型未提供该值;仅在没有精确类型分支且联合含 null 时归一为 None, + # 保留 anyOf[string, null] 等以空字符串为合法值的语义。 + if not exact and isinstance(value, str) and not value.strip(): + if any(item.get("type") == "null" for item in alternatives): + return None candidates = [] for option in exact or alternatives: try: diff --git a/tests/test_agent_api_arguments.py b/tests/test_agent_api_arguments.py new file mode 100644 index 000000000..b8abb6158 --- /dev/null +++ b/tests/test_agent_api_arguments.py @@ -0,0 +1,30 @@ +""" +验证 agent API 参数合同对空字符串联合参数的类型归一行为。 +""" + +from app.agent.api.arguments import _normalize_value + + +def test_empty_string_with_null_union_normalizes_to_none(): + """ + 空字符串在 anyOf[integer, null] 下表示模型未提供该值,归一为 None。 + """ + schema = {"anyOf": [{"type": "integer"}, {"type": "null"}]} + assert _normalize_value("", schema, {}) is None + assert _normalize_value(" ", schema, {}) is None + + +def test_empty_string_with_string_union_is_preserved(): + """ + anyOf[string, null] 的空字符串本身是合法值,不应被改成 None。 + """ + schema = {"anyOf": [{"type": "string"}, {"type": "null"}]} + assert _normalize_value("", schema, {}) == "" + + +def test_integer_union_still_accepts_integer(): + """ + 正常整数输入不受空字符串容错影响。 + """ + schema = {"anyOf": [{"type": "integer"}, {"type": "null"}]} + assert _normalize_value(3, schema, {}) == 3