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