Skip to content

Does Runware client terminate the connection? #156

@ymb812

Description

@ymb812

I run Runware in the FastAPI lifespan on startup. Everything works correctly, but after a while (hours/days) the image generation crashes with the error ConnectionError: Invalid API key. Get one at https://my.runware.ai/signup. However, after restarting the application, everything works correctly again.

Traceback (most recent call last):
  File "/app/code/api/managers/pipline_handler.py", line 289, in process_image_generation
    image_url = await self.runware_manager.generate_image(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
    )
    ^
  File "/app/code/api/managers/runware_manager.py", line 45, in generate_image
    images = await self.client.imageInference(requestImage=request_image)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/runware/base.py", line 472, in imageInference
    raise e
  File "/usr/local/lib/python3.13/site-packages/runware/base.py", line 254, in imageInference
    await self.ensureConnection()
  File "/usr/local/lib/python3.13/site-packages/runware/base.py", line 1130, in ensureConnection
    raise ConnectionError(
    ...<2 lines>...
    )
ConnectionError: Invalid API key. Get one at https://my.runware.ai/signup

FastAPI lifespan

@asynccontextmanager
async def lifespan(_: FastAPI):
    runware_manager = RunwareManager()
    await runware_manager.startup()

    yield {
        'RunwareManager': runware_manager,
    }

Here is my class that I work with

from runware import Runware, IImageInference
from settings.settings import settings


class RunwareManager:
    def __init__(
            self,
            max_retries=3,
            positive_prompt='hyper realistic photos: ',
            negative_rompt='black leather',
            model='runware:101@1',
    ):
        self.client = Runware(api_key=settings.runware_api_key.get_secret_value())
        self.max_retries = max_retries
        self.positive_prompt = positive_prompt
        self.negative_rompt = negative_rompt
        self.model = model


    async def startup(self) -> None:
        await self.client.connect()


    async def generate_image(
        self,
        positive_prompt: str,
        height: int,
        width: int,
    ) -> str:
        request_image = IImageInference(
            outputFormat='JPG',
            outputType='URL',
            positivePrompt=self.positive_prompt + positive_prompt,
            negativePrompt=self.negative_rompt,
            height=height,
            width=width,
            model=self.model,
            scheduler='DPM++ 3M Karras',
            checkNsfw=False,
            steps=90,
            CFGScale=10.0,
            clipSkip=0,
            numberResults=1,
        )
        images = await self.client.imageInference(requestImage=request_image)
        return images[0].imageURL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions