azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

$return not working for python kafka binding

Open nicholas-ebert opened this issue 2 years ago • 5 comments

I'd like to be able to return the result of my kafka event processor and avoid using func.Out if possible. I have a function.json which is setup like this:

{
      "scriptFile": "main.py",
      "bindings": [
        {
          "type": "kafkaTrigger",
          "name": "kevent",
          "topic": "%InputTopicName%",
          "brokerList": "%BrokerList%",
          "username": "%ConfluentCloudUserName%",
          "password": "%ConfluentCloudPassword%",
          "consumerGroup" : "%ConsumerGroup%",
          "protocol": "saslSsl",
          "authenticationMode": "plain"
        },
        {
          "type": "kafka",
          "direction": "out",
          "name": "$return",
          "topic": "%OutputTopicName%",
          "brokerList": "%BrokerList%",
          "username": "%ConfluentCloudUserName%",
          "password": "%ConfluentCloudPassword%",
          "protocol": "saslSsl",
          "authenticationMode": "plain"
        }
    ]
}

And my code looks like this:

from azure.functions import KafkaEvent
import json

def main(kevent: KafkaEvent) -> str:
    event = json.loads(kevent.get_body().decode("utf-8"))
    message = json.loads(event["Value"])

    print(message)

    return message

Note the "name": "$return", in the kafka output binding. Running this with func start --python, I get these errors:

[2022-07-19T20:37:41.386Z] Executing 'Functions.formatter' (Reason='(null)', Id=cb9b2351-f72c-4862-8b1e-b544c6455220)
[2022-07-19T20:37:42.426Z] Executed 'Functions.formatter' (Failed, Id=cb9b2351-f72c-4862-8b1e-b544c6455220, Duration=1039ms)
[2022-07-19T20:37:42.427Z] System.Private.CoreLib: Exception while executing function: Functions.formatter. System.Private.CoreLib: Result: Failure
[2022-07-19T20:37:42.427Z] Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure.functions.kafka.KafkaConverter'>" for Python type "str"
[2022-07-19T20:37:42.427Z] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@4/4.0.4653/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 434, in _handle__invocation_request
[2022-07-19T20:37:42.427Z]     return_value = bindings.to_outgoing_proto(
[2022-07-19T20:37:42.427Z]   File "/usr/local/Cellar/azure-functions-core-tools@4/4.0.4653/workers/python/3.9/OSX/X64/azure_functions_worker/bindings/meta.py", line 150, in to_outgoing_proto
[2022-07-19T20:37:42.427Z]     datum = get_datum(binding, obj, pytype)
[2022-07-19T20:37:42.427Z]   File "/usr/local/Cellar/azure-functions-core-tools@4/4.0.4653/workers/python/3.9/OSX/X64/azure_functions_worker/bindings/meta.py", line 108, in get_datum
[2022-07-19T20:37:42.427Z]     raise TypeError(
[2022-07-19T20:37:42.427Z] .

After some investigation, it seems this happens because KafkaConverter.encode always raises NotImplementedError('Output bindings are not supported for Kafka'), which obviously isn't true, right? The output binding works perfectly well when I use a func.Out variable, why can't it handle an $return?

nicholas-ebert avatar Jul 19 '22 20:07 nicholas-ebert

Hi @vrdmr could you please help with this issue?

Ved2806 avatar Jul 26 '22 06:07 Ved2806

@shrohilla - Could you please take a look at this issue?

vrdmr avatar Aug 05 '22 18:08 vrdmr

@nicholas-ebert is this a feature request or a bug?

r00ta avatar Sep 29 '22 22:09 r00ta

@nicholas-ebert is this a feature request or a bug?

Based on the fact that this seems to be completely unsupported currently I suppose it's a feature request. But if this feature isn't intended to be present it'd be great if this was made more obvious.

Since $return is used by other output bindings it's not crazy that someone would try with the kafka binding, and the error that gets generated above is not obvious to the issue at all.

nicholas-ebert avatar Oct 03 '22 18:10 nicholas-ebert

@shrohilla - Could you please take a look at this issue? And can this be supported or not?

vrdmr avatar Oct 10 '22 15:10 vrdmr