Expand description
§bentoml
An unofficial async Rust client for BentoML services (GitHub).
BentoML services expose their @bentoml.api methods as HTTP POST endpoints whose
route is derived from the method name. Because endpoints are defined dynamically
per-service, this crate doesn’t generate typed bindings: instead you name a route
with client.endpoint(route) (or client.task(route) for an @bentoml.task) and
call it over your own serde types.
§Features
- Typed calls:
endpoint(route).invoke(&payload)over your ownserderequest and response types, with no codegen or per-service bindings. - Async task queues:
client.task(route)submits@bentoml.taskjobs, then poll status, fetch results, retry, or cancel through aTaskHandle. The synchronous and task surfaces are distinct handle types, socallandsubmitcan’t be mixed. - File and streaming I/O:
multipart/form-datafile inputs, raw-binary root inputs, binary responses, and chunked streaming endpoints (featurestream). - Resilient transport: exponential-backoff retries via
reqwest-middleware, bearer-token auth, an optional per-request timeout, and a cheap-to-cloneArc-backed client.
§Usage
Add the dependency:
[dependencies]
bentoml = "0.5"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"] }The minimum supported Rust version (MSRV) is 1.91.
use bentoml::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize)]
struct SummarizeRequest { text: String }
#[derive(Deserialize)]
struct SummarizeResponse { summary: String }
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::builder()
.with_base_url("http://localhost:3000")
.build()?;
let resp: SummarizeResponse = client
.endpoint("summarize")
.invoke(&SummarizeRequest { text: "...".into() })
.await?;
println!("{}", resp.summary);
Ok(())
}A handle names the route once; calls are made on it. See examples/ for
runnable examples.
§Capabilities
The handle’s kind mirrors the BentoML decorator, and decides which methods are
available — call is not callable on a task handle, nor submit on an api handle.
A Client::endpoint(route) handle (@bentoml.api) covers the synchronous surface:
call/call_bytes/call_multipart: send a JSON, raw-binary, ormultipart/form-databody (built withmultipart::Multipart), returning anEndpointReplyyou read as.json::<R>(),.bytes(),.text(), or (featurestream).stream()— so input and output encodings are chosen independently.invoke: the JSON-in, JSON-out shorthand —invoke(&p)deserializes the response for you, equivalent tocall(&p).await?.json().await?.
A Client::task(route) handle (@bentoml.task) covers the async task surface:
submit/submit_bytes/submit_multipart: submit a JSON, raw-binary, ormultipart/form-datatask input; return aTaskHandleforstatus,wait,retry,cancel, and a result read asjson::<R>()/bytes()/text().
EndpointReply::stream() yields a ByteStream of response chunks; decode it with
.text(), .lines(), or .json::<T>().
The Client itself provides health checks: is_ready / is_live and
wait_until_ready.
These are gated by feature flags:
rustls-tls(default): HTTPS via Rustls.native-tls: HTTPS via the platform-native TLS stack.stream: response streaming viaEndpointReply::stream.tracing:#[tracing::instrument]spans on request methods, including anyx-request-idas arequest_idfield.
§Changelog
See CHANGELOG.md for release notes and version history.
§License
Licensed under the MIT License.
Re-exports§
pub use crate::task::TaskEndpoint;
Modules§
- multipart
- The
Multipartbody builder and itsPart. - stream
stream - Streaming response endpoints.
- task
- Async task-queue endpoints (
@bentoml.task).
Structs§
- Client
- An async client for a single BentoML service.
- Client
Builder - A builder for
Client. - Endpoint
- A handle to a synchronous service endpoint (
@bentoml.api), pairing a route with itsClient. - Endpoint
Reply - A successful response from an endpoint, ready to be read in a chosen format.
Enums§
- Error
- The error type returned by client operations.
Type Aliases§
- Result
- A convenient alias for results returned by this crate.