Asynchronous Enhancement
The async enhancement method is the asynchronous counterpart of the sync
enhancement flow in 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 = await client.request_ad_async(
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 asynchronous 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 async pipeline still
gets a prompt and can continue without complex error branching.
Exception behavior
With raise_exception=True (default), request_ad_async 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.
Minimal async integration pattern
- Python
import asyncio
from adstractai import Adstract
from adstractai.models import AdRequestContext
async def main() -> None:
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 = await client.request_ad_async(
prompt="How can I improve user retention?",
context=context,
)
prompt_for_model = result.prompt
await client.aclose()
asyncio.run(main())
Next steps
- Continue to EnhancementResult for result object details.
- Continue to Asynchronous Acknowledgment to complete the reporting cycle in async flows.
- Continue to Exception for full SDK exception behavior.