Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/guidellm/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def info(self) -> dict[str, Any]:
"""
...

@abstractmethod
async def reset(self) -> None:
"""
Reset the connection object. This is useful for backends that
reuse connections or have state that needs to be cleared.
"""
...

async def validate(self):
"""
Handle final setup and validate the backend is ready for use.
Expand All @@ -126,6 +134,8 @@ async def validate(self):
): # type: ignore[attr-defined]
pass

await self.reset()

@abstractmethod
async def check_setup(self):
"""
Expand Down
11 changes: 10 additions & 1 deletion src/guidellm/backend/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ def info(self) -> dict[str, Any]:
"chat_completions_path": CHAT_COMPLETIONS_PATH,
}

async def reset(self) -> None:
"""
Reset the connection object. This is useful for backends that
reuse connections or have state that needs to be cleared.
For this backend, it closes the async client if it exists.
"""
if self._async_client is not None:
await self._async_client.aclose()

async def check_setup(self):
"""
Check if the backend is setup correctly and can be used for requests.
Expand Down Expand Up @@ -361,7 +370,7 @@ def _get_async_client(self) -> httpx.AsyncClient:

:return: The async HTTP client.
"""
if self._async_client is None:
if self._async_client is None or self._async_client.is_closed:
client = httpx.AsyncClient(
http2=self.http2,
timeout=self.timeout,
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/mock_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def model(self) -> Optional[str]:
def info(self) -> dict[str, Any]:
return {}

async def reset(self) -> None:
pass

async def prepare_multiprocessing(self):
pass

Expand Down