Principle:Bentoml BentoML HTTP Client Testing
| Knowledge Sources | |
|---|---|
| Domains | |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
A design pattern for testing BentoML services programmatically using auto-generated HTTP clients. BentoML clients introspect the service's OpenAPI schema to dynamically generate typed methods matching each API endpoint, enabling programmatic testing without manual HTTP request construction.
Description
HTTP client testing in BentoML leverages the schema-driven client generation pattern. When a BentoML service starts, it exposes an OpenAPI specification at its root endpoint. The client libraries (SyncHTTPClient and AsyncHTTPClient) fetch this schema and dynamically create Python methods that mirror the service's API endpoints.
This approach provides several benefits:
- Type-safe testing -- client methods accept and return the same Python types as the service methods, catching type mismatches early.
- No boilerplate -- developers do not need to manually construct HTTP requests, set headers, or parse responses; the client handles all serialization.
- Sync and async support --
SyncHTTPClientprovides blocking calls for simple test scripts, whileAsyncHTTPClientintegrates withasynciofor concurrent testing. - Schema validation -- since the client is generated from the OpenAPI spec, it inherently validates that the service's API contract has not changed unexpectedly.
The typical testing workflow is:
- Start the service locally (via
bentoml serveor in a test fixture). - Create a client pointing to the service URL.
- Call the auto-generated methods corresponding to each API endpoint.
- Assert on the returned values.
Usage
Use HTTP client testing when:
- Writing integration tests for a BentoML service.
- Running smoke tests against a deployed service.
- Building end-to-end test pipelines that programmatically invoke service endpoints.
A typical test pattern:
import bentoml
# Start the service (e.g., in a test fixture)
# bentoml serve service:MyService --port 3000
# Create a client
with bentoml.SyncHTTPClient("http://localhost:3000") as client:
result = client.predict(text="Hello, world!")
assert "label" in result
assert result["score"] > 0.5
Theoretical Basis
The HTTP client testing pattern applies the proxy pattern combined with schema-driven code generation: the client acts as a local proxy for the remote service, with its interface derived automatically from the service's published API schema.
The abstract pattern is as follows:
HTTP_CLIENT_TESTING(service_url):
SCHEMA DISCOVERY:
1. GET {service_url}/docs.json (OpenAPI specification)
2. Parse endpoint definitions: routes, parameter types, return types
METHOD GENERATION:
FOR each endpoint E in schema:
GENERATE method E.name(params: E.input_types) -> E.output_type:
1. SERIALIZE params to HTTP request body
2. POST to {service_url}/{E.route}
3. DESERIALIZE response body to E.output_type
4. RETURN typed result
TEST EXECUTION:
client = SyncHTTPClient.from_url(service_url)
result = client.{endpoint_name}(inputs)
ASSERT result matches expected output
Key theoretical properties:
- Contract testing -- the client validates that the service adheres to its declared API contract at runtime.
- Dynamic generation -- methods are created at runtime from the schema, so the client always matches the current service version.
- Abstraction -- the client hides HTTP transport details, allowing tests to focus on business logic assertions.