You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python版Service Bus Trigger Azure Function State字段类型转换失败

Python Azure Service Bus Trigger 报错:ValueError: cannot convert value of field 'State' in trigger metadata into int

问题现象

执行Service Bus Trigger Azure Function时触发以下错误:

Exception while executing function: Functions.servicebus_trigger_name Result: Failure
Exception: ValueError: cannot convert value of field 'State' in trigger metadata into int: invalid literal for int() with base 10: 'Out'

完整栈追踪:

Stack:   File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 637, in _handle__invocation_request
args[pb.name] = bindings.from_incoming_proto(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/bindings/meta.py", line 189, in from_incoming_proto
return binding.decode(datum, trigger_metadata=metadata)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/functions/servicebus.py", line 258, in decode
return cls.decode_single_message(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/functions/servicebus.py", line 336, in decode_single_message
state=cls._decode_trigger_metadata_field(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/functions/meta.py", line 182, in _decode_trigger_metadata_field
return cls._decode_typed_data(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/functions/meta.py", line 166, in _decode_typed_data
raise ValueError(

原因分析

该错误是由于Azure Functions Python SDK在解析Service Bus消息的触发器元数据时,错误地将State字段按整数类型解析,但实际收到的元数据值是字符串'Out',类型不匹配导致转换失败。这通常是SDK版本存在类型映射bug,或是元数据字段定义变更未同步到SDK解析逻辑中。

解决方案

  • 升级Azure Functions SDK:执行以下命令升级到最新稳定版,新版本大概率修复了该类型解析问题:
    pip install --upgrade azure-functions
    
  • 规避元数据依赖:如果你的函数逻辑不需要使用State字段,可以修改函数参数,只接收消息内容而非完整元数据。例如,将函数定义从包含元数据参数改为仅接收ServiceBusMessage:
    import azure.functions as func
    
    def main(msg: func.ServiceBusMessage):
        # 处理消息内容,不依赖State元数据
        message_body = msg.get_body().decode('utf-8')
        print(f"Received message: {message_body}")
    
  • 临时应急修复(不推荐生产环境):若升级后问题仍存在,可临时修改本地SDK的解析逻辑:找到azure/functions/servicebus.py中解析State的代码段,将类型从int改为str,或添加异常捕获逻辑,但此方案仅作临时过渡,后续仍需依赖官方修复。

内容的提问来源于stack exchange,提问作者Joe Plumb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:54:58