pythonazureazure-functionsazure-durable-functions

Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure Exception: TypeError: Object of type coroutine is not JSON serializable


I am attempting to create a Durable Functions app that utilizes an HTTP trigger API using Python in Azure Function App. However, I am encountering the error 'Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure Exception: TypeError: Object of type coroutine is not JSON serializable'. Below is the code:

import azure.functions as func
import azure.durable_functions as df
from common_config.scripts import shared_config as sc
from xml_remit_gen import app_controller

myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

# Set configurations function (now synchronous)
def set_configurations(req_body):
    sc.GV_JOBCHAIN = req_body.get('GV_JOBCHAIN')
    sc.GV_XML_SPLIT_REQ = req_body.get('GV_XML_SPLIT_REQ')
    sc.GV_SKIP_THRESHOLD_LIMIT_CHECK_YN = req_body.get('GV_SKIP_THRESHOLD_LIMIT_CHECK_YN')
    sc.BATCH_SIZE = req_body.get('BATCH_SIZE')
    sc.PARAM_MINPROCESS_ID = req_body.get('INSERT_PROCESS_ID')


# HTTP-Triggered Function with a Durable Functions Client binding
@myApp.route(route="orchestrators/xml_remit_gen", methods=["POST"])
@myApp.durable_client_input(client_name="client")
async def http_xml_remit_gen(req: func.HttpRequest, client: df.DurableOrchestrationClient):
    try:
        req_body = req.get_json()
        set_configurations(req_body)  # Call the sync function
        print(req_body)
        # Assuming no additional arguments are required for start_new
        instance_id = await client.start_new("hello_orchestrator", req_body)
        response = client.create_check_status_response(req, instance_id)
    except Exception as e:
        return func.HttpResponse(f"Error starting orchestration: {str(e)}", status_code=500)



# Orchestrator
@myApp.orchestration_trigger(context_name="context")
async def hello_orchestrator(context: df.DurableOrchestrationContext):
    req_body = context.get_input()
    await df.call_activity("run_xml_remit_gen", req_body)  # Pass req_body directly
    return req_body  # Not necessary to return req_body here


# Activity
@myApp.activity_trigger(input_name="reqBody")
async def run_xml_remit_gen(reqBody: str):
    await app_controller.xml_remit_gen_main()  # Assuming this is a valid async function
    return reqBody

i've tried debugging line by line and i saw that its throwing error when i reach below line response = client.create_check_status_response(req, instance_id)


Solution

  • TypeError: Object of type coroutine is not JSON serializable

    The error "TypeError: Object of type coroutine is not JSON serializable," indicates you're trying to serialize a coroutine object into JSON.

    The issue might be you're trying to create a response using client.create_check_status_response(req, instance_id) within the http_xml_remit_gen function. This function is asynchronous, and you're awaiting it, which means it returns a coroutine object.

    Modified Code:

    @myApp.route(route="orchestrators/xml_remit_gen", methods=["POST"])
    @myApp.durable_client_input(client_name="client")
    async def http_xml_remit_gen(req: func.HttpRequest, client: df.DurableOrchestrationClient):
        try:
            req_body = req.get_json()
            set_configurations(req_body)  # Call the sync function
            print(req_body)
                    instance_id = await client.start_new("hello_orchestrator", req_body)
            # Await the response creation separately
            response = await client.create_check_status_response(req, instance_id)
            return response  # Return the response after awaiting
        except Exception as e:
            return func.HttpResponse(f"Error starting orchestration: {str(e)}", status_code=500)