Skip to content

Bug: _producer not setup during NATS direct publishing #852

Description

@Jonas1312

Hi,

Describe the bug

I get this error:

Traceback (most recent call last):
  File "/Users/jonasm/colot/mre.py", line 39, in <module>
    publisher.publish_run_status(
  File "/Users/jonasm/colot/mre.py", line 23, in publish_run_status
    async_to_sync(self.apublish_run_status)(progress_message)
  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/site-packages/asgiref/sync.py", line 277, in __call__
    return call_result.result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/site-packages/asgiref/sync.py", line 353, in main_wrap
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jonasm/copilot/mre.py", line 29, in apublish_run_status
    await publisher.publish(
  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/site-packages/faststream/nats/publisher.py", line 34, in publish
    assert self._producer, "Please, setup `_producer` first"  # nosec B101
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Please, setup `_producer` first

How to reproduce
Include source code:

from asgiref.sync import async_to_sync
from faststream.nats import NatsBroker
from pydantic import BaseModel, Field


class RunProgressMessage(BaseModel):

    progress_percentage: float = Field(ge=0, le=100)
    remaining_time_seconds: float | None = Field(None, ge=0)


class NATSRunStatusPublisher:

    def __init__(self, broker: NatsBroker, nats_subject_template: str) -> None:
        self._broker = broker
        self._nats_subject_template = nats_subject_template

    def publish_run_status(self, progress_message: RunProgressMessage) -> None:
        async_to_sync(self.apublish_run_status)(progress_message)

    async def apublish_run_status(self, progress_message: RunProgressMessage) -> None:
        nats_subject = self._nats_subject_template.format(run_id="some_run_id")
        publisher = self._broker.publisher(nats_subject)
        await publisher.publish(
            subject=nats_subject,
            message=progress_message,
        )


if __name__ == "__main__":
    broker = NatsBroker()
    publisher = NATSRunStatusPublisher(broker, nats_subject_template="runStatus.{run_id}")

    publisher.publish_run_status(
        RunProgressMessage(progress_percentage=50, remaining_time_seconds=50)
    )

Expected behavior

I'm not sure if my code is correct or not, but to me, it should send the message.

Observed behavior

  File "/opt/anaconda3/envs/llm-processor/lib/python3.11/site-packages/faststream/nats/publisher.py", line 34, in publish
    assert self._producer, "Please, setup `_producer` first"  # nosec B101
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Please, setup `_producer` first

Environment

Running FastStream 0.2.5 with CPython 3.11.4 on Darwin

Additional context
Provide any other relevant context or information about the problem here.

Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions