Adstract Client
Adstract is the main client class used to connect your application to Adstract.
It centralizes authentication, network configuration, retry behavior, transport
injection, and cleanup.
Class role
At the class level, Adstract is responsible for:
- holding your API authentication context;
- applying global client configuration to requests;
- managing sync and async transport clients;
- enforcing input validation at client setup; and
- providing deterministic lifecycle cleanup (
close/aclose).
Initialization
- Python
from adstractai import Adstract
client = Adstract(
api_key="your-api-key",
base_url=None,
timeout=100,
retries=0,
backoff_factor=0.5,
max_backoff=8.0,
http_client=None,
async_http_client=None,
wrapping_type="xml",
)
Configuration reference
-
api_key- Type:
str | None - Purpose: Authenticates your client with Adstract.
- Behavior:
- If omitted, SDK reads
ADSTRACT_API_KEYfrom environment. - Must be a non-empty string with minimum length requirements.
- If omitted, SDK reads
- Type:
-
base_url- Type:
str | None - Purpose: Overrides the default Adstract API host for this client instance.
- Behavior:
- If
None, SDK uses the default platform URL.
- If
- Type:
-
timeout- Type:
float - Purpose: Sets request timeout behavior for transport operations.
- Behavior:
- Applied to internally created sync and async HTTP clients.
- Type:
-
retries- Type:
int - Purpose: Controls automatic retry attempts for retryable failures.
- Behavior:
- SDK caps supported retry attempts.
- Values above the supported cap fall back to SDK default.
- Type:
-
backoff_factor- Type:
float - Purpose: Configures exponential retry delay growth.
- Behavior:
- Higher values increase wait time between retry attempts.
- Type:
-
max_backoff- Type:
float - Purpose: Sets an upper bound for retry delay.
- Behavior:
- Prevents retry backoff from growing without limit.
- Type:
-
http_client- Type:
httpx.Client | None - Purpose: Injects a custom synchronous transport client.
- Behavior:
- If omitted, SDK creates and owns an internal sync client.
- If provided, caller owns lifecycle and should close it.
- Type:
-
async_http_client- Type:
httpx.AsyncClient | None - Purpose: Injects a custom asynchronous transport client.
- Behavior:
- If omitted, SDK creates and owns an internal async client.
- If provided, caller owns lifecycle and should close it.
- Type:
-
wrapping_type- Type:
"xml" | "plain" | None - Purpose: Configures how Adstract instructs the LLM to wrap ad content, which directly affects how the final response should be analyzed.
- Behavior:
- Supported values:
xml,plain. - If omitted, defaults to
xml.
- Supported values:
- Type:
Validation behavior
- Initialization raises
ValidationErrorif:- API key is missing/invalid after environment resolution.
wrapping_typeis outside supported values.
Client lifecycle
- Use
close()for sync cleanup. - Use
aclose()for async cleanup. - If you pass custom HTTP clients, your application owns their lifecycle.
Transport ownership model
Adstract can run in two transport modes:
- Internal transport mode:
- SDK creates sync/async clients from your timeout and retry config.
- SDK closes those clients when you call
close/aclose.
- External transport mode:
- You pass
http_clientand/orasync_http_client. - Your application is responsible for their lifecycle and shutdown.
- You pass
Minimal client example
- Python
from adstractai import Adstract
client = Adstract(api_key="your-api-key")
Next steps
- Continue to Initialize Your Integration to instantiate the client in the runtime flow.
- Continue to AdRequestConfiguration for request-level configuration fields.
- Continue to EnhancementResult for output handling.
- Continue to Exception for error type behavior.