From 72f999f8f7a8e8984c7ee2c1ccd50d1f8b3cf419 Mon Sep 17 00:00:00 2001 From: Haisam Abbas Date: Wed, 20 May 2026 13:56:43 +0500 Subject: [PATCH] refactor image provider HTTP handling --- nanobot/providers/image_generation.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/nanobot/providers/image_generation.py b/nanobot/providers/image_generation.py index 501b98fd2..b267f5c00 100644 --- a/nanobot/providers/image_generation.py +++ b/nanobot/providers/image_generation.py @@ -219,7 +219,10 @@ class ImageGenerationProvider(ABC): *, headers: dict[str, str], body: dict[str, Any], + client: httpx.AsyncClient | None = None, ) -> httpx.Response: + if client is not None: + return await client.post(url, headers=headers, json=body) if self._client is not None: return await self._client.post(url, headers=headers, json=body) async with httpx.AsyncClient(timeout=self.timeout) as c: @@ -390,10 +393,11 @@ class AIHubMixImageGenerationClient(ImageGenerationProvider): model_path = _aihubmix_model_path(model) url = f"{self.api_base}/models/{model_path}/predictions" try: - response = await client.post( + response = await self._http_post( url, headers={**headers, "Content-Type": "application/json"}, - json=body, + body=body, + client=client, ) except httpx.TimeoutException as exc: raise ImageGenerationError("AIHubMix image generation timed out") from exc @@ -706,22 +710,16 @@ class MiniMaxImageGenerationClient(ImageGenerationProvider): body.update(self.extra_body) - client = self._client or httpx.AsyncClient(timeout=self.timeout) - try: - return await self._generate_with_client(client, body, headers) - finally: - if self._client is None: - await client.aclose() + return await self._generate_with_client(body, headers) async def _generate_with_client( self, - client: httpx.AsyncClient, body: dict[str, Any], headers: dict[str, str], ) -> GeneratedImageResponse: url = f"{self.api_base}/image_generation" try: - response = await client.post(url, headers=headers, json=body) + response = await self._http_post(url, headers=headers, body=body) except httpx.TimeoutException as exc: raise ImageGenerationError("MiniMax image generation timed out") from exc except httpx.RequestError as exc: