Repository navigation
Conversation
PostHog.Emails.batch/2 sends a template or code content to up to 100 recipients and 4 MiB per request, one request after another, with an idempotency key per recipient that every retry reuses, and returns a result per recipient in order. A request that fails as a whole returns a BatchError with every recipient's result. Ported from the Python SDK's emails.batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
posthog-elixir Compliance ReportDate: 2026-11-25T15:42:35.555770+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
… the end A read by key now only fills in the message id and never changes the recipient's status. A request answered idempotency_key_in_flight, delivery_unavailable or api_error leaves its recipients unconfirmed, also after a later refusal. Every batch, on success and on error, reads each unconfirmed recipient without a message id at its end. An exception from the HTTP client after the first request returns a BatchError with every result, the batch stays bound to the instance it started on, a stop before a request leaves its recipients not_attempted, and each request body is built only when its request starts. The batch reuses the single send's form checks and retry driver. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/batch-email-send
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/batch-email-send # Conflicts: # lib/posthog/emails.ex
…d retries A stop during the reads by key now fails the batch after a successful request too. A 429 without a body fails the request instead of leaving it unconfirmed. A later lost answer keeps a recipient's first error and records the later one. The backoff stops doubling past its cap, so a high max_retries can't overflow it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/batch-email-send
…read Retries now stop an hour after the first attempt, so none can land after PostHog forgets the idempotency key. A stop that lands while a read by key is in flight now returns client_closed. A lost retry no longer replaces the last answer PostHog gave in later_error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/batch-email-send
…tled batch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/batch-email-send # Conflicts: # lib/posthog/emails.ex
… key Drop the reads by key at the end of a batch. An unconfirmed email keeps its idempotency key, and its error's fix says to send it again with the same key within 24 hours. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An unconfirmed email's fix now says to resend it unchanged with the same key within 24 hours of the key's first use. An answer without a result per recipient after a lost answer lands in later_error. The key docs say a reused key for another email is refused with idempotency_key_reused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The retry window is checked again after each wait, so a pause during the backoff can't send a retry past the hour. The batch docs say a request in flight when the instance stops returns its own outcome. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…try window A wait that both overran the hour and saw the instance stop returned the last answer. It now returns client_closed, as the next attempt would have. The overrun test asserts that PostHog received a single request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A batch now takes at most 100 recipients and goes out in one request. More than 100 is refused before anything is sent. PostHog's refusals, such as 413 too_large, come back as the error, and the 413 fix says to split the recipients. The chunk loop, the 4 MiB sizing, not_attempted and later_error are gone: the last answer decides each result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PostHog answers an oversized batch with 413 content_too_large and its own fix. The SDK now keeps that fix and fills in the code and a fix only for a 413 without a body. The hour and shutdown tests assert the requests PostHog received. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…swer is refused Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sends Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irmed answer Retries also stop when a wait ends exactly at the one-hour cutoff. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ent the email Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, through retries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bodyless 413 default no longer claims nothing was sent, empty fields no longer override the defaults, and a test sends a batch over 4 MiB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…used retry A stop during the backoff now says the SDK sent no further request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…used batch The message of a refused retry no longer says an answer was lost after a 503, and a top-level idempotency_key asks for one key per recipient. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 413 body whose fields aren't text keeps the SDK's advice, and an improper list of recipients is refused as not a list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-recipient batch as too large Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es as too large Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…one-recipient 413 to resending Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s second request Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Silthus
marked this pull request as ready for review
November 25, 2026 11:27
There was a problem hiding this comment.
PostHog Review · Standard · reviewed 1249191: 1 Should fix. This pull request is large, so the review ran in 4 parts with less depth than usual.
New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.
See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.
…confirmed A send's retry window now also ends when the system clock passes the hour, so a host suspension counts. A batch 409 whose body can't be read leaves every recipient unconfirmed. Batch warnings dedupe in one pass, in the order PostHog named them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… a stop Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o out Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
marandaneto
approved these changes
Nov 25, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
PostHog answers the same 404 for an unknown template_id, a project without batch sends, and a server without the route. The batch error now says the template ID is wrong or batch sends aren't available for the project, with a fix to send each recipient on its own, and never suggests updating PostHog. A batch with template_key names only the batch sends. A 404 whose code is api_error now fails its recipients too, since PostHog sent none of them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 404 to a retry after a lost answer no longer says PostHog sent no email, since the first try may have queued it. A 404 reads its code and field the way the error does, so a code that isn't text or a field that isn't text gets the same advice. A batch with template_key says sending email from code may be unavailable too, and advises sending each recipient on its own only where the project can. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Sending the same email to many people now takes one call:
PostHog.Emails.batch(recipients: [...], template_id: ...), one email per recipient, with:template_dataper recipient for a template. Today a caller loops oversend/2, one request per recipient. The server route,POST /api/messaging/send/email_batch/, comes in PostHog/posthog#138258 and PostHog/posthog#138025, behindworkflows-email-batch-sends.It ports
emails.batchfrom the Python SDK (PostHog/posthog-python#1291), the reference: the same limits, statuses, error codes, retries and 24-hour replay window. The Node change (PostHog/posthog-js#5847) shares the limits, statuses and codes, but sends a batch once without retries.Stacked on #264 (
scheduled_at), since both changelib/posthog/emails.ex.Impact
A batch replaces a loop of single sends for digests, invites and announcements, and keeps each recipient's result readable. The SDK carries no flag of its own; the server flag decides who can send batches, and a project without it answers 404, which the SDK names.
What changes
PostHog.Emails.batch/2, in the template form (:template_id, shared:template_data, per-recipient:template_data) and the code form (:template_keyand the contentsend/2takes). A batch takes no:cc,:bcc,:attachmentsor:scheduled_at. Before any request, the SDK refuses those fields, a recipient without:to, an unknown recipient field, and two recipients with one key. Each refusal names the recipient's position.invalid_recipientsbefore anything is sent, and itsfixsays to split the recipients into batches of 100 or fewer. The SDK checks no byte size: PostHog's limits decide.:idempotency_key, given or made by the SDK, that every retry reuses. Each attempt waits 45 seconds, or the configured timeout when longer, since the server waits up to 30 seconds on its email pipeline. Retries follow the single send's rules, and stop an hour after the first attempt, timed on both the monotonic and the system clock so a host suspension counts, whatever:max_retriessays, so no retry reaches PostHog after its 24-hour replay window.PostHog.Emails.BatchEmailResultinPostHog.Emails.BatchResult, in the order of:recipients. Each result carriesstatus("queued","failed"or"unconfirmed"),message_id,replayedanderror.unconfirmedwith the error of the refusal and the advice to resend with the same key.idempotency_key_in_flight,delivery_unavailableorapi_errorin its body), every recipient isunconfirmed.delivery_unavailable,idempotency_key_in_flightorapi_errorisunconfirmed, since PostHog may still queue it. Any other slot error isfailed.unconfirmedemail's error has afixthat says to send it again, unchanged, with the same:idempotency_keywithin 24 hours of the key's first use, so it goes out once. The SDK makes no request after the batch, so nothing fills in a message ID PostHog didn't name.{:error, %PostHog.Emails.BatchError{}}. It carries the error fieldsErrorhas, plus every result and the warnings.unconfirmed, since PostHog refused it before queuing any. So does another 4xx without one of the uncertain codes above.content_too_largemeans the request is too large, and comes back as the error with PostHog's fix to split the recipients into smaller batches. For a 413 without a body, the SDK fills in that code and a fix.content_too_largemeans the content or:template_datashared by every recipient passes PostHog's limit, where splitting doesn't help. It comes back with PostHog's own words and no split advice.unconfirmed. The message keeps PostHog's words and says the email may still go out, and the fix says to resend unchanged with the same key, then gives PostHog's own advice if it refuses again. A stop during the backoff says the SDK sent no further request.unconfirmed, since it may be a key still in flight.batch_unavailableis not retried, and itsfixsays to send each email withsend/2and its key. A 404 that names no field says PostHog found no template with this ID, or batch sends aren't available for the project, and fails every recipient. Itsfixsays to check the template ID, then send each recipient on its own. It never suggests updating PostHog. After a lost answer, the recipients stayunconfirmedwith their keys.recipients__37__to, which is the caller's position.client_closed. A request already in flight when it stops returns its own outcome, as the docs say.BatchResult.warningsand are logged once.PostHog.Emails.Error, since nothing was sent.Where it differs from Python
{:error, ...}tuples with Elixir codes (invalid_recipients,invalid_recipient,invalid_idempotency_key,invalid_template_data), notTypeErrororValueError. A recipient error's:fieldisrecipients__<i>__<field>, the same form as the server's.niland an empty:cc,:bccor:attachmentscount as left out, as onsend/2.Warning
🟡 Merge risk: medium, 🔴 at full rollout · one-way door once released: the public names
PostHog.Emails.batch/2,BatchResult,BatchEmailResult,BatchErrorand the three statusesbatch/2is new, no existing call changes, and the server flag is on for an internal project only. Once a maintainer approves the release, Hex fixes the names above.send/2andget/2keep their request bytes, timeout and parse. A send's retries now stop an hour after its first attempt, which only a:max_retriesfar above the default reaches; such a send returns its last error where base went on. Status reads and attachment uploads keep base's retries.client_closed, forsend/2andget/2too. On base the retry went out through the new instance. After a try PostHog didn't confirm, a stopped or restartedsend/2now says the email may still go out, and its fix names the key to resend it with, where base said no email was sent. Some private helpers inPostHog.Emailsbecame@doc falsefunctions so the batch module can reuse them; the existing send tests cover them.workflows-email-batch-sends, which answers 404 while off. None in the SDK: fix forward.Rollout
failedemails in the server's email status rows through each flag stage, and batch route 5xx, 409 and 404 answers.💚 How did you test it?
Unit tests in
test/posthog/emails_batch_test.exsthrough theAPI.Mockclient, written before the code:content_too_largewithout it, and a refused field;unconfirmed, with no request after the batch;batch_unavailablenot retried, and a 404 without a field, after a lost answer too;unconfirmed;Review fixes added tests for a host suspension during a retry wait, a 409 without a readable body, warnings kept in PostHog's order, an uncertain slot without a detail, a status read retrying past the hour, a stop after a 503 or a lost answer on
send/2, a recipient field namednil, a 429 without a body, a 409 after a lost answer, retries stopping an hour after the first attempt, going on when the wall clock jumps, stopping after a wait that overran the hour and returningclient_closedwhen the instance stopped during it, a 429 codedapi_error, and the scheduled send's clock after a 429.Breaking the main guarded behaviours one at a time made at least one test fail each. Locally,
mix compile --warnings-as-errors,mix format --check-formatted,mix credo --strict,mix posthog.public_api --checkandmix testpass, except one public API task test that needs git inside the test container.Live run on PostHog Cloud US at this head, template form: a batch of two in one request came back
queuedwith two message IDs in order, the same batch with the same keys returned the same IDs and sent nothing more, and both emails arrived once with their own per-recipient data. A shadow diff of 20 send and status scenarios on the base and this head records identical requests and results.Known issues
max_retries: 0for such a rerun.{:ok, ...}rather thanclient_closed, and a lost answer returnsnetwork_error. Each result holds what PostHog answered, and the docs say so.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Written by Claude Opus 5.5 (
claude-opus-5-5, high effort) in Claude Code, ported test-first from the Python reference. Reviews run on GPT-6.2 Sol through Hedge proof.batch_unavailableis read from the body and not retriedBatchErrorexception with theErrorfields copied flat, plusresultsandwarnings. Rejected: results onErrorInvolvement: no grilling rounds, no prototypes; a review panel (design, anti-overengineering), fixes reads and final reads on GPT-6.2 Sol, one approved by Michael after the loop stopped and the later ones run by the agent; live runs captured.
🤖 Generated with Claude Code
How did you test this code?
Merge card: medium, one-way door · 3 rounds, 27 final reads · gaps: judge (🤖 agent d-9def84b3) · 2 known issues · 1735 min opened to ready
8448213(reused from277f179)8448213(reused from18e5ee6)5091cfbto84482138448213Not checked: none recorded
Full receipt: https://artifacts.posthog.com/a/WprqVOInQknMGq4TFpbDBz