Synchronous Enhancement
The synchronous enhancement method is the primary way to request an ad-enhanced
prompt from Adstract. By default it raises on failure. Pass raise_exception=False
for fallback-first behavior: your application always receives an EnhancementResult
and a usable prompt output.
Method signature
- Python
result = client.request_ad(
prompt="How can I improve user retention?",
context=context,
)
Inputs
-
prompt- Type:
str - Meaning: Original user prompt before enhancement.
- Role: Source text Adstract attempts to enhance.
- Type:
-
context- Type:
AdRequestContext - Meaning: Request context object.
- Required fields:
session_iduser_agentuser_ip
- Type:
-
optional_context- Type:
OptionalContext | None - Meaning: Optional targeting context for improved ad relevance.
- Optional fields:
country(ISO 3166-1 alpha-2 code, e.g."US")region(e.g."California")city(e.g."San Francisco")asn(Autonomous System Number)age(integer 0–120)gender("male","female", or"other")
- Type:
-
raise_exception- Type:
bool - Default:
True - Meaning: Controls error handling behavior.
- Behavior:
True(default): raises exceptions on any failure.False: captures failures inEnhancementResult.errorand returns original prompt as fallback.
- Type:
For full context details, see AdRequestContext. For targeting details, see OptionalContext.
Output
This method returns an EnhancementResult.
result.prompt- Enhanced prompt on success.
- Original prompt on fallback (when
raise_exception=False).
result.successTruewhen enhancement succeeds.Falsewhen fallback path is used.
result.errorNoneon success.- Captured exception on fallback failure path.
result.session_id- Session identifier used for this request.
result.ad_response- Parsed response payload when available.
For output object details, see EnhancementResult.
Behavior model
The method flow is:
- Build and validate request payload.
- Send enhancement request to Adstract.
- If enhancement is successful and an enhanced prompt is returned, return it.
- If the response status is
rejected, raise (or capture)PromptRejectedError. - If the response status is
no_fill, raise (or capture)NoFillError. - If any other failure occurs, raise (or capture) the appropriate exception.
When raise_exception=False, fallback result means your app still gets a prompt
and can continue its normal LLM flow with minimal branching logic.
Exception behavior
With raise_exception=True (default), request_ad raises on all failures:
MissingParameterError— required parameters missingNetworkError— transport/connectivity failureAuthenticationError— authentication/authorization failureDuplicateAdRequestError— the provided message already has an ad requestRateLimitError— rate limit exceeded after retriesServerError— server error after retriesPromptRejectedError— prompt not suitable for ad injectionNoFillError— no ad inventory availableAdEnhancementError— enhancement failed for another reason
With raise_exception=False, all errors are captured in EnhancementResult.error.
For full exception reference, see Exception.
Integration pattern
- Python
- Raising — exceptions thrown on failure
- Fallback — errors captured in result
from adstractai import Adstract
from adstractai.models import AdRequestContext
from adstractai.errors import AdSDKError
client = Adstract(api_key="your-api-key")
context = AdRequestContext(
session_id="session-abc",
user_agent="Mozilla/5.0 (X11; Linux x86_64)",
user_ip="203.0.113.10",
)
try:
result = client.request_ad(
prompt="How can I improve user retention?",
context=context,
)
prompt_for_model = result.prompt
except AdSDKError:
prompt_for_model = "How can I improve user retention?"
from adstractai import Adstract
from adstractai.models import AdRequestContext
client = Adstract(api_key="your-api-key")
context = AdRequestContext(
session_id="session-abc",
user_agent="Mozilla/5.0 (X11; Linux x86_64)",
user_ip="203.0.113.10",
)
result = client.request_ad(
prompt="How can I improve user retention?",
context=context,
raise_exception=False,
)
if not result.success:
print(f"Enhancement failed: {result.error}")
prompt_for_model = result.prompt
Next steps
- Continue to Asynchronous Enhancement for the async enhancement counterpart.
- Continue to EnhancementResult for result object details.
- Continue to Synchronous Acknowledgment to complete the reporting cycle.