Installation
GRAVITY_API_KEY in your server environment. Only dependency is httpx.
Quick start
Add a few lines to your existing streaming chat endpoint. The ad request runs in parallel with your LLM call — zero added latency.Constructor
The client reuses its HTTP connection pool across calls. Use
async with Gravity() as g: or call await gravity.close() for explicit cleanup.
get_ads()
get_ads() reads gravity_context (session, user, and device) from the posted request body and adds the end-user IP from the request headers. It does not invent a device — your browser client must call gravityContext() and post the result, and your backend must leave it in the body it forwards.
Return types
.to_dict() methods that serialize to the camelCase JSON shape renderers expect.
Message handling
The SDK sends the last 2 conversational messages to the Gravity API for contextual ad matching. Only messages with recognized roles are included:user,assistant,system,developer,model(Gemini’s alias forassistant)
tool, function, ipython) are filtered out — they typically contain structured data rather than natural language.
PII hashing
The SDK provideshash_pii() for SHA-256 hashing of emails and phone numbers, matching the normalization used by the Gravity publisher pixel and the advertiser-side conversion pipeline.
- Email:
strip().lower() - Phone: digits only (e.g.
"+1 (555) 123-4567"→"15551234567")
HashedIdentity dict with only the keys that successfully hashed (email_hash, hashed_phone), so it’s safe to spread into a user dict. email_hash is the canonical email field — it matches the email_hash used by the Index /search API.
Lead forms
When a campaign is configured as alead_form ad unit, the AdResponse includes a lead_form field:
lead_form.submit_url.

