在 AWS Step Function 状态机中,如何启用 JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS

问题描述

调用负载中包含 float('nan') 的状态机时,结果提示启用 JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS,因为 Non-standard token 'NaN'

import boto3
import json
client = boto3.client('stepfunctions',endpoint_url=endpoint_url)
client.start_sync_execution(stateMachineArn='state_machine_arn',name='new-execution',input=json.dumps({'foo': float('nan')})

(截断)

InvalidExecutionInput                     Traceback (most recent call last)
...
~/opt/miniconda3/envs/pandars38/lib/python3.8/site-packages/botocore/client.py in _make_api_call(self,operation_name,api_params)
    674             error_code = parsed_response.get("Error",{}).get("Code")
    675             error_class = self.exceptions.from_code(error_code)
--> 676             raise error_class(parsed_response,operation_name)
    677         else:
    678             return parsed_response

InvalidExecutionInput: An error occurred (InvalidExecutionInput) when calling the StartSyncExecution operation: Invalid State Machine Execution Input: 'Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow'

(包装)

InvalidExecutionInput: 
    An error occurred (InvalidExecutionInput) when calling the StartSyncExecution operation: 
    Invalid State Machine Execution Input: 
    'Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow'

同样的异常也发生在 numpy np.nan 上。

解决方法

根据官方 JSON 规范,NaN 不是有效的 JSON 值。 https://www.json.org/json-en.html

Step Functions 仅支持符合官方 JSON 规范的标准令牌。

我建议将您的 NaN 编码为 null。