Exceptions
All SDK exceptions inherit from AdSDKError.
The exception model is split into:
- validation/input exceptions raised before or during request construction;
- transport/HTTP exceptions raised while communicating with Adstract;
- response parsing exceptions raised when server payloads are invalid; and
- ad enhancement exceptions raised when the enhancement pipeline does not produce an ad.
- ad acknowledgment exceptions raised when the acknowledgment pipeline fails.
SDK design approach
Adstract is designed for straightforward integration with minimal application code changes.
- By default,
request_adandrequest_ad_asyncraise on any failure. - Set
raise_exception=Falseto enable fallback-first behavior: the method always returns anEnhancementResultand captures any failure inerror. - This keeps control flow simple and predictable when graceful degradation is preferred.
- It also preserves fallback behavior so publisher applications can continue running close to their original pre-Adstract flow when enhancement fails.
Base exception
AdSDKError- Base type for all SDK-specific exceptions.
- Can include:
status_coderesponse_snippet
Validation and input exceptions
-
ValidationError- Raised for invalid SDK inputs or invalid request construction state.
- Typical trigger points:
- invalid
api_keyduringAdstract(...)initialization; - invalid
wrapping_type; - invalid
user_agentor other parameter validation failures.
- invalid
-
MissingParameterError- Raised when required request parameters are missing/empty.
- Typical trigger points:
- missing
session_id; - missing/empty
user_agent; - missing/empty
user_ip.
- missing
Network and Authentication exceptions
-
NetworkError- Raised for transport-level failures (timeouts/connectivity).
- Includes
original_errorfor underlying client failure context.
-
RateLimitError- Raised after retry exhaustion when server keeps returning
429.
- Raised after retry exhaustion when server keeps returning
-
AuthenticationError- Raised on authentication/authorization failures.
- Typical HTTP statuses:
- enhancement:
400,401,403 - acknowledgment:
400,401,403
- enhancement:
- Typical trigger points:
- bad API key format (
400); - missing API key (
401); - invalid API key (
401); - revoked API key (
403); - platform or publisher not active (
403).
- bad API key format (
Ad enhancement exceptions
-
AdEnhancementError- Raised when the ad enhancement pipeline did not produce a usable result for a reason not covered by a more specific subclass.
-
PromptRejectedError- Raised when the ad system determined the prompt content is not appropriate
for advertisement placement (
status='rejected').
- Raised when the ad system determined the prompt content is not appropriate
for advertisement placement (
-
NoFillError- Raised when the prompt was suitable for ad injection, but no matching ad
inventory was available to fill the opportunity (
status='no_fill').
- Raised when the prompt was suitable for ad injection, but no matching ad
inventory was available to fill the opportunity (
-
DuplicateAdRequestError- Raised when the provided message already has an ad request.
- Typical HTTP status:
409.
Ad acknowledgment exceptions
-
AdAcknowledgmentError- Base type for acknowledgment-specific failures.
-
AdResponseNotFoundError- Raised when the referenced
ad_response_iddoes not exist. - Typical HTTP status:
404.
- Raised when the referenced
-
UnsuccessfulAdResponseError- Raised when the referenced ad response was not created by a successful enhancement.
- Typical HTTP status:
406.
-
DuplicateAcknowledgmentError- Raised when an acknowledgment already exists for the given ad response.
- Typical HTTP status:
409.
Other exceptions
-
UnexpectedResponseError- Raised when response payload cannot be safely consumed.
- Typical trigger points:
- unexpected
4xxclient error statuses (outside auth handling); - invalid JSON response body;
- response JSON does not match expected SDK model shape;
- acknowledgment success responses that do not match
AdAckResponse.
- unexpected
-
ServerError- Raised after retry exhaustion for
5xxserver responses.
- Raised after retry exhaustion for
Fallback method behavior
When raise_exception=False is passed to request_ad or request_ad_async,
failures are captured inside EnhancementResult.error instead of raising to
the caller.
Exception handling pattern
- Python
- Fallback — errors captured in result
- Raising — exceptions thrown on failure
from adstractai import Adstract
from adstractai.models import AdRequestContext
from adstractai.errors import (
AdEnhancementError,
AuthenticationError,
DuplicateAdRequestError,
MissingParameterError,
NetworkError,
NoFillError,
PromptRejectedError,
RateLimitError,
ServerError,
)
client = Adstract(api_key="your-api-key")
result = client.request_ad(
prompt="Explain unit economics",
context=AdRequestContext(
session_id="sess-600",
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
user_ip="203.0.113.40",
),
raise_exception=False,
)
if isinstance(result.error, AuthenticationError):
print("Authentication failed")
elif isinstance(result.error, MissingParameterError):
print("Missing required request field")
elif isinstance(result.error, DuplicateAdRequestError):
print("Message already has an ad request")
elif isinstance(result.error, RateLimitError):
print("Rate limited")
elif isinstance(result.error, ServerError):
print("Server error")
elif isinstance(result.error, NetworkError):
print("Network failure")
elif isinstance(result.error, PromptRejectedError):
print("Prompt rejected — not suitable for ad injection")
elif isinstance(result.error, NoFillError):
print("No fill — no ad inventory available")
elif isinstance(result.error, AdEnhancementError):
print("Ad enhancement failed")
from adstractai import Adstract
from adstractai.models import AdRequestContext
from adstractai.errors import (
AdEnhancementError,
AuthenticationError,
DuplicateAdRequestError,
MissingParameterError,
NetworkError,
NoFillError,
PromptRejectedError,
RateLimitError,
ServerError,
)
client = Adstract(api_key="your-api-key")
try:
result = client.request_ad(
prompt="Explain unit economics",
context=AdRequestContext(
session_id="sess-600",
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
user_ip="203.0.113.40",
),
)
except AuthenticationError:
print("Authentication failed")
except MissingParameterError:
print("Missing required request field")
except DuplicateAdRequestError:
print("Message already has an ad request")
except RateLimitError:
print("Rate limited")
except ServerError:
print("Server error")
except NetworkError:
print("Network failure")
except PromptRejectedError:
print("Prompt rejected — not suitable for ad injection")
except NoFillError:
print("No fill — no ad inventory available")
except AdEnhancementError:
print("Ad enhancement failed")
Next steps
- Continue to Initialize Your Integration to begin the integration flow with a client instance.
- Continue to Synchronous Acknowledgment for sync reporting behavior.
- Continue to Asynchronous Acknowledgment for async reporting behavior.
- Continue to Important and Disclaimers for compliance and policy-critical guidance.