pub struct ClientBuilder<K = NoApiKey, P = NoProvider> { /* private fields */ }Expand description
Type-state builder for DefaultClient.
The two type parameters K and P track whether the required fields have
been supplied:
KisNoApiKeyuntilapi_keyis called, thenWithApiKey.PisNoProvideruntilprovideris called, thenWithProvider.
build is only available when both are WithApiKey and
WithProvider. Attempting to call it in any other state is a compile
error.
All optional knobs (base_url, timeout, transport, …) are available
at any point in the chain and do not affect the type-state.
§Examples
use liter_llm::ClientBuilder;
let client = ClientBuilder::new()
.api_key("sk-…")
.provider("openai")
.timeout(std::time::Duration::from_secs(30))
.build()?;Implementations§
Source§impl<K, P> ClientBuilder<K, P>
impl<K, P> ClientBuilder<K, P>
Sourcepub fn api_key(self, key: impl Into<String>) -> ClientBuilder<WithApiKey, P>
pub fn api_key(self, key: impl Into<String>) -> ClientBuilder<WithApiKey, P>
Set the API key for authentication.
Accepts any value that converts into a SecretString (including
&str and String). The secret is zeroed on drop.
Calling this method transitions the K type parameter from
NoApiKey to WithApiKey.
Sourcepub fn provider(
self,
provider_name: impl Into<String>,
) -> ClientBuilder<K, WithProvider>
pub fn provider( self, provider_name: impl Into<String>, ) -> ClientBuilder<K, WithProvider>
Set the provider by name or model-hint string.
The string is forwarded to provider auto-detection in the same way
that the model_hint argument to DefaultClient::new works. Use
a slash-prefixed name (e.g. "openai", "groq/llama3-70b",
"anthropic") or a bare model name.
Calling this method transitions the P type parameter from
NoProvider to WithProvider.
Sourcepub fn base_url(self, url: impl Into<String>) -> Self
pub fn base_url(self, url: impl Into<String>) -> Self
Override the provider base URL for all requests.
Trailing slashes are trimmed automatically. When set, provider auto-detection is bypassed and all requests go to this URL.
Sourcepub fn max_retries(self, retries: u32) -> Self
pub fn max_retries(self, retries: u32) -> Self
Set the maximum number of retries on 429 / 5xx responses (default: 3).
Sourcepub fn max_response_bytes(self, limit: usize) -> Result<Self>
pub fn max_response_bytes(self, limit: usize) -> Result<Self>
Limit retained HTTP bodies; successful streams keep their existing frame bounds.
§Errors
Returns an error when the limit is zero.
Sourcepub fn transport(self, config: TransportConfig) -> Self
pub fn transport(self, config: TransportConfig) -> Self
Set HTTP connection pooling, TCP keepalive, DNS caching, and version selection.
Sourcepub fn load_env(self, enabled: bool) -> Self
pub fn load_env(self, enabled: bool) -> Self
Enable or disable automatic API key loading from environment variables.
When true, the client will attempt to read the provider’s designated
environment variable (e.g. OPENAI_API_KEY) if no key was set via
api_key. Defaults to false in ClientBuilder
(unlike ClientConfigBuilder
which defaults to true) because the type-state design signals that
the key should be provided explicitly.
Sourcepub fn credential_provider(self, provider: Arc<dyn CredentialProvider>) -> Self
pub fn credential_provider(self, provider: Arc<dyn CredentialProvider>) -> Self
Set a dynamic credential provider for token-based or refreshable auth.
When configured, the client calls resolve() before each request
instead of using the static API key for authentication.
Source§impl ClientBuilder<WithApiKey, WithProvider>
impl ClientBuilder<WithApiKey, WithProvider>
Sourcepub fn build(self) -> Result<DefaultClient>
pub fn build(self) -> Result<DefaultClient>
Consume the builder and construct a DefaultClient.
This method is only available when both api_key
and provider have been called. The
compiler enforces this at the type level.
§Errors
Returns LiterLlmError::Authentication if the provider requires an
environment variable that is unset (only relevant when the API key
supplied via [api_key] is an empty string and load_env is true).
Returns LiterLlmError::Http if the underlying HTTP client cannot be
constructed.