相同的输入,调试的时候可以,使用的时候却说函数数据类型不正确



这到底是为什么啊,有没有大佬告诉我 :sob:

这个是你自己定义的函数吗,你看看执行详情里面,在哪一步出的问题

这里的操作是向后端发送大模型需要调用的API,实现智能控制,发送脚本的代码,就是这里调试的代码为(ip已注释):import json
import urllib.request
import urllib.error

def api(api_config: dict) → str:
“”"
通用的API调用函数,参数通过字典配置

参数:
    api_config: 包含API配置信息的字典,至少需要包含 'api' 键作为API路径。
                其他可选键包括 'operation', 'device', 'intensity', 'user' 等。

返回:
    API调用结果字符串
"""
if 'API' not in api_config:
    return json.dumps({"success": False, "message": "API配置字典缺少 'API' 键"})

url = "**********:****" + api_config['API']
data = {"status": True}  # 强制添加 status

# 动态添加其他参数
if 'operation' in api_config:
    data['operation'] = api_config['operation']
if 'deviceName' in api_config:
    data['deviceName'] = api_config['deviceName']
if 'intensity' in api_config:
    data['intensity'] = api_config['intensity']
if 'username' in api_config:
    data['username'] = api_config['username']

try:
    # 准备请求
    headers = {'Content-Type': 'application/json'}
    req = urllib.request.Request(
        url,
        data=json.dumps(data).encode('utf-8'),
        headers=headers,
        method='POST'
    )

    # 发送请求
    with urllib.request.urlopen(req, timeout=5) as resp:
        resp_text = resp.read().decode('utf-8')
        try:
            return resp_text
        except:
            return '{"success":true,"message":"操作成功"}'

except Exception as e:
    return json.dumps({
        "success": False,
        "message": str(e)
    })

这里我是把这个脚本单独拎出来测,因为使用的时候我看执行详情也是说它报错,前面的给的数据也没有任何问题

前置的传参默认不是字典类型的,数据类型都先搞成字符串,在函数库里再转成你要的字典就可以了