Implementation:Vllm project Vllm Connections
| Knowledge Sources | |
|---|---|
| Domains | HTTP, Infrastructure |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Provides HTTP connection utilities for fetching remote resources with both synchronous and asynchronous client support.
Description
This module defines the HTTPConnection class that wraps requests.Session (synchronous) and aiohttp.ClientSession (asynchronous) for making HTTP requests. It provides convenience methods for fetching bytes, text, JSON, and downloading files, with URL validation, custom vLLM user-agent headers, timeout control, and optional session reuse for connection pooling. A module-level global_http_connection singleton is provided for shared use across vLLM.
Usage
Use this module when vLLM needs to download remote model weights, configuration files, tokenizer assets, or any other HTTP-accessible resources. The global instance provides a centralized, reusable HTTP client with proper error handling and consistent request headers.
Code Reference
Source Location
- Repository: vllm
- File: vllm/connections.py
- Lines: 1-189
Signature
class HTTPConnection:
def __init__(self, *, reuse_client: bool = True) -> None: ...
def get_sync_client(self) -> requests.Session: ...
async def get_async_client(self) -> aiohttp.ClientSession: ...
def get_response(self, url: str, *, stream: bool = False,
timeout: float | None = None, ...) -> requests.Response: ...
async def get_async_response(self, url: str, *, timeout: float | None = None,
...) -> aiohttp.ClientResponse: ...
def get_bytes(self, url: str, ...) -> bytes: ...
async def async_get_bytes(self, url: str, ...) -> bytes: ...
def get_text(self, url: str, ...) -> str: ...
async def async_get_text(self, url: str, ...) -> str: ...
def get_json(self, url: str, ...) -> str: ...
async def async_get_json(self, url: str, ...) -> str: ...
def download_file(self, url: str, save_path: Path, ...) -> Path: ...
async def async_download_file(self, url: str, save_path: Path, ...) -> Path: ...
global_http_connection = HTTPConnection()
Import
from vllm.connections import HTTPConnection, global_http_connection
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | str | Yes | HTTP or HTTPS URL to fetch from |
| reuse_client | bool | No | Whether to reuse HTTP session across requests (default True) |
| stream | bool | No | Enable streaming response for large downloads (default False) |
| timeout | float or None | No | Request timeout in seconds (default None for no timeout) |
| extra_headers | Mapping[str, str] or None | No | Additional HTTP headers to include |
| allow_redirects | bool | No | Whether to follow HTTP redirects (default True) |
| save_path | Path | download_file only | Local file path for saving downloaded content |
| chunk_size | int | No | Chunk size in bytes for file downloads (default 128) |
Outputs
| Name | Type | Description |
|---|---|---|
| bytes | bytes | Raw response content (get_bytes / async_get_bytes) |
| text | str | Response body as text (get_text / async_get_text) |
| json | str | Parsed JSON response (get_json / async_get_json) |
| path | Path | Path to downloaded file (download_file / async_download_file) |
Usage Examples
from vllm.connections import global_http_connection
# Synchronous: fetch JSON configuration from a remote URL
config = global_http_connection.get_json(
"https://huggingface.co/meta-llama/Llama-2-7b/resolve/main/config.json",
timeout=30.0,
)
# Synchronous: download a file to disk
from pathlib import Path
saved = global_http_connection.download_file(
"https://example.com/model.safetensors",
save_path=Path("/tmp/model.safetensors"),
timeout=300.0,
chunk_size=8192,
)
# Asynchronous: fetch text content
import asyncio
async def fetch():
text = await global_http_connection.async_get_text(
"https://example.com/tokenizer.json"
)
return text