You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
airflow.providers.apache.kafka ConsumeFromTopicOperator cannot be used for pipelining because it returns None. #56494
As of Airflow 3.0.6, the ConsumeFromTopicOperator takes the name+path to a function and applies it to all messages in a batch, but the results of this function call are discarded and the method returns None. Rather than dropping the result of the apply_function, the results of the apply_function could be accumulated and returned so that other tasks could use the results.
Use case/motivation
It would be nice to be able to pipeline operators and functions like we can with other Python tasks and other Operators. An example:
defmy_service_prefilter(message):
# This will get messages from my-topic-1 and my-topic-2. We will extract and return some dicts.ifmessageandmessage.value():
decoded=json.loads(message.value().decode("utf-8")
ifdecoded['some_value']:
returndecodedreturnNone
@task.pythondefhandle_batch(decoded_items: list[dict]):
foritemindecoded_items:
ifitem:
do_something_with_item(item)
message_batch=ConsumeFromTopicOperator(
task_id="consume_pending_kafka_messages",
topics=["my-topic-1", "my-topic-2", ],
apply_function="my_service.prefilter_function",
)
message_batch>>handle_batch
Written differently:
Kafka message stream -> ConsumeFromTopicOperator -> [message returned by the apply_function * batch size] -> whatever downstream task.
I initially wrote this as a bug report after spending a while with a colleague who had treated the ConsumeFromTopicOperator like other operators, but realized that returning None is the intended behavior.
I recognize that the Airflow documentation explicitly states that Airflow is not made to be used in the streaming data sense, but in this case if we need to transform some data and distribute it to other tasks we need to build our own operators instead of just reusing the existing Operators. The Operator documentation seems to suggest that Operators be composable.
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
@JosephCatrambone-Gridware, thanks for opening this issue. Can you tell me a little bit more about the use-case? This might be a perfect scenario for event-driven scheduling using Assets and AssetWatchers!
JosephCatrambone-Gridware commented on Oct 9, 2025
Certainly. We had a ConsumeFromTopicOperator which was gathering messages from a bunch of Kafka topics, dropping some irrelevant ones, then processing them en-masse to take advantage of bulk-parallel processing, and pushing the resultant list of N elements to the next task downstream.
The structure looked very similar to the elements in the first message:
but it seems like it would be a straightforward change to the ConsumerFromTopicOperator to just do a gather of the resulting values and return that list or empty.
Looking at the AssetWatchers and Asset it feels like those might possibly be a good fit, though I'd need to investigate more. I think I remember trying them early-on but had an issue with things not triggering when items were added to the Kafka queue. Perhaps it's worth revisiting.
JosephCatrambone-Gridware commented on Oct 14, 2025
Thanks for the reply. On a similar note, it appears as though the ProduceToTopicOperator doesn't have a way to take any input from other tasks in the graph. One can provide a function to the method, but if we have, say, an existing batch of tasks which produce a list of dicts then the following isn't possible:
withDAG(...) asdag:
@task.pythondefmy_sample_producer():
return [{"key": "foo", "value":"..."}, {"key": "..."}, ...]
prod=ProduceToTopicOperator(
topic=OUTPUT_TOPIC,
producer_function=this_method_will_be_called_but_we_cant_read_from_other_tasks,
)
my_sample_producers() >>prod# This will not work because the prod operator can't access any context or input.
I feel like it still makes sense from an admittedly fuzzy aesthetics/composability perspective for Operators to be able to accept and/or produce data from tasks, but I can respect not wanting to have two ways to accomplish this.
A small code change which injects the context into the function before it's called looks like it would resolve this.
classProduceToTopicOperator(BaseOperator):
...
defexecute(self, context) ->None:
self.kwargs['context'] =context# New line. Obviously with more nuance, like checking if context is present, etc.
...
I don't trust myself enough to understand all the repercussions of these kinds of changes, but if it's as simple as it looks (it probably isn't) then it would add a lot of functionality for a small number of code changes.
Update: I forgot that get_current_context() is an option and might do precisely what's needed in this context. Will look into it and report back.
Update 2: get_current_context() allows us to write a wrapper function that works quite nicely and makes ProduceToTopicOperator usable!
I feel like there are some ergonomic tweaks that could make get_current_context or, better yet, the entire map_to_kafka_producer function, unnecessary, but as it is I'm satisfied.
Activity
boring-cyborg commentedon Oct 9, 2025
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
jroachgolf84 commentedon Oct 9, 2025
@JosephCatrambone-Gridware, thanks for opening this issue. Can you tell me a little bit more about the use-case? This might be a perfect scenario for event-driven scheduling using
AssetsandAssetWatchers!JosephCatrambone-Gridware commentedon Oct 9, 2025
Certainly. We had a ConsumeFromTopicOperator which was gathering messages from a bunch of Kafka topics, dropping some irrelevant ones, then processing them en-masse to take advantage of bulk-parallel processing, and pushing the resultant list of N elements to the next task downstream.
The structure looked very similar to the elements in the first message:
We were trying to hack our way around this with
but it seems like it would be a straightforward change to the ConsumerFromTopicOperator to just do a gather of the resulting values and return that list or empty.
Looking at the AssetWatchers and Asset it feels like those might possibly be a good fit, though I'd need to investigate more. I think I remember trying them early-on but had an issue with things not triggering when items were added to the Kafka queue. Perhaps it's worth revisiting.
JosephCatrambone-Gridware commentedon Oct 14, 2025
Another use case that is not possible with the ConsumeFromTopicOperator:
jroachgolf84 commentedon Oct 15, 2025
I think that this is the perfect use-case for event driven scheduling. We have some basic support for this functionality with Kafka (https://airflow.apache.org/docs/apache-airflow-providers-apache-kafka/stable/message-queues/index.html). If this doesn't meet your needs, this is a great opportunity for a significant contribution to the project.
JosephCatrambone-Gridware commentedon Oct 23, 2025
Thanks for the reply. On a similar note, it appears as though the ProduceToTopicOperator doesn't have a way to take any input from other tasks in the graph. One can provide a function to the method, but if we have, say, an existing batch of tasks which produce a list of dicts then the following isn't possible:
I feel like it still makes sense from an admittedly fuzzy aesthetics/composability perspective for Operators to be able to accept and/or produce data from tasks, but I can respect not wanting to have two ways to accomplish this.
A small code change which injects the context into the function before it's called looks like it would resolve this.
I don't trust myself enough to understand all the repercussions of these kinds of changes, but if it's as simple as it looks (it probably isn't) then it would add a lot of functionality for a small number of code changes.
Update: I forgot that
get_current_context()is an option and might do precisely what's needed in this context. Will look into it and report back.Update 2:
get_current_context()allows us to write a wrapper function that works quite nicely and makes ProduceToTopicOperator usable!I feel like there are some ergonomic tweaks that could make get_current_context or, better yet, the entire map_to_kafka_producer function, unnecessary, but as it is I'm satisfied.
Kafka: Return consumed message results on request (#69740)