Reverse ETL: apply outputs
An apply output sends a job's rows to a vendor's API, one operation per row, and records what happened to each
row. Use it to send conversions or events, or to create, update and remove objects such as campaigns. Both engines
support it (kind: DuckDBJob and kind: SparkJob).
Each row is sent in a chunk, its outcome (applied or failed) is stored, and a re-run never sends an applied row
again.
Example
This job takes conversions from the caller, keeps those with a value, and uploads them to Google Ads.
kind: DuckDBJob
version: 1
name: upload_conversions
description: Upload offline click conversions to a Google Ads account.
spec:
config:
customer_id: {type: string, required: true, pattern: "^[0-9]{10}$"}
validate_only: {type: boolean, default: false}
secrets:
developer_token: {type: string, required: true}
client_id: {type: string, required: true}
client_secret: {type: string, required: true}
refresh_token: {type: string, required: true}
input: # the rows to send, from --input
columns:
gclid: {type: string, required: true, max_length: 200}
conversion_action: {type: string, required: true}
conversion_date_time: {type: string, required: true}
conversion_value: {type: number, minimum: 0}
currency_code: {type: string, enum: [USD, EUR, GBP]}
connections:
ads_api:
provider: google_ads
developer_token: "{{ secrets.developer_token }}"
client_id: "{{ secrets.client_id }}"
client_secret: "{{ secrets.client_secret }}"
refresh_token: "{{ secrets.refresh_token }}"
steps:
- name: valid_conversions # each row becomes one conversion upload
sql: SELECT * FROM input WHERE conversion_value > 0
outputs:
- name: upload_conversions
connection: ads_api # where
step: valid_conversions # or sql:
operation: # what
service: ConversionUploadService
method: upload_click_conversions
arguments: {customer_id: "{{ config.customer_id }}", validate_only: "{{ config.validate_only }}"}
apply: # how rows are sent and judged
chunk_size: 2000
on_row_error: fail
policy: # how calls retry and how fast they go
retry: {max_attempts: 5, backoff: exponential}
rate_limit: {requests: 5, per: 1s}
log: # optional: a line per failed row, once the rows are sent
sql: SELECT gclid, outcome.error_code AS code FROM upload_conversions WHERE outcome.status = 'failed'
message: "gclid {gclid} rejected: {code}"
level: warning
runtime:
state:
sqlite: state.db # keeps every row's outcome between runs
failed_job_policy: retry
# once: create the state tables (the job's runtime.state database)
streamwright state migrate --url sqlite:///state.db
# a dry run: Google checks the rows and records none
streamwright run conversions.yaml --secrets secrets.yaml --input conversions.json \
--set customer_id=1234567890 --set validate_only=true
# the upload
streamwright run conversions.yaml --secrets secrets.yaml --input conversions.json --set customer_id=1234567890
# every row's status, attempts, result and error: by job id, or by the run's request id (req=sw_... in its log lines)
streamwright state show --url sqlite:///state.db JOB_ID
streamwright state show --url sqlite:///state.db --request-id sw_...
What it does:
operation:names the vendor operation, the way a call step names a read: a connection of a connector, and{service, method, arguments}.streamwright connectors NAMEand the connector's guide list its apply operations (Google Ads: the*Service.mutate_*methods and the conversion uploads).- Each row of
valid_conversionsbecomes one conversion upload, sent in chunks of 2,000 rows per call. - Every row's outcome is stored in the state store, and failed rows are logged.
- With
failed_job_policy: retry, the next run sends only the rejected rows again (see Rejected rows).
Row lifecycle
- Checked. A row that cannot be sent fails on its own and is never sent (codes below).
- Pinned. The rows are committed to the batch's outbox together with the batch, with the output's rendered
arguments. A retry applies the same rows with the same arguments. - Sent in chunks. Each chunk is marked in flight before its call (write-ahead).
- Judged. After the call, every row's outcome is stored: what the vendor returned (for example the new
campaign's
resource_name), or its error code and message. When the vendor reports errors per row (for example Google Ads partial failures), a rejected row fails alone. A call the vendor rejects as a whole fails all its rows. - Done. A completed batch is never applied again. With
--job-key, a given piece of work runs once.
Delivery is at least once, and honest about it: when a call is cut short, its rows are not marked applied or failed. They stay in flight until the outcome is known (Unknown outcomes).
Each row has a status, shown by streamwright state show --url URL JOB_ID (or --request-id ID) with its attempts,
result and error:
| Status | Meaning | On the next run |
|---|---|---|
pending | Committed, not sent yet | Sent |
in_flight | Its call was cut short: nobody knows if the vendor applied it | See Unknown outcomes |
applied | The vendor accepted it | Never sent again |
failed | Rejected, or never sendable | Stays failed, unless the batch is retried (below) |
With on_row_error: fail (the default), a rejected row makes the run exit 1, unless an error rule
says otherwise. The outcomes are kept either way.
Options
Apply settings
| Key | What it does | Default |
|---|---|---|
chunk_size | Rows per call | The operation's max_chunk (also the maximum) |
on_row_error | What a rejected row no error rule matches does. fail: the run exits 1 (see rejected rows). continue: the row is kept as failed without failing the run | fail |
error_threshold | How many rejected rows let through (continue) a batch may have, as a number (10) or a share (5%) of its rows not ignored. Above it, the batch fails | No limit |
error_rules | What known rejections mean (error rules) | None |
nulls | A NULL field: error fails the row, skip_field leaves the field out, clear clears the field at the vendor | error |
on_unknown | Rows whose outcome is unknown: hold stops for you to decide, reconcile asks the connector (see unknown outcomes) | hold |
allow_remove | Lets rows use the operation's destructive actions (op: remove) | false |
An apply output's policy: takes retry and rate_limit.
Error rules
Tag the rejections you know, and say what each one means. The first rule that matches a rejected row wins:
apply:
on_row_error: fail # a rejection no rule matches fails the batch
error_rules:
- tag: click_too_old
match: {code: [EXPIRED_EVENT, TOO_RECENT_EVENT]}
then: ignore # known and expected: never fails the batch
- tag: event_too_old # Meta: code 100, subcode 2804003
match: {code: "100", subcode: 2804003}
then: ignore
- tag: token_expired
match: {code: "190"}
then: fail # stops the output at once
match:code(one or a list),message_pattern(a regular expression on the message, or a list: any), or a field of the vendor's error that the connector's guide lists (Google Adscategory, Metasubcode, ...). Every key must match.then:ignore(recorded and tagged, never fails the batch, not counted byerror_threshold),continue(counted byerror_threshold) orfail(fails the batch at once; rows not sent yet stay pending).- A row no rule matches is tagged
unclassifiedand followson_row_error. Some connectors add rules of their own after yours, e.g. a bad token fails the batch. - The vendor's code and message are kept as they came;
state show, the console andlog:show the tag beside them. To leave known errors out of a log:WHERE outcome.status = 'failed' AND outcome.then <> 'ignore'.
Row columns
Each column is a field of the vendor's object; the connector's guide says which (campaign.name,
network_settings.target_google_search, ...). When the operation has several actions (create, update,
remove), a column op picks the row's action; the default is the first.
Before anything is sent, a row fails on its own, and is never sent, when:
| Code | Why |
|---|---|
INVALID_ACTION | Its op is not one of the operation's actions |
REMOVE_NOT_ALLOWED | Its op is destructive and the output does not set allow_remove: true |
NULL_VALUE | A field is NULL under nulls: error |
Supported connectors
| Connector | What an output sends | Guide |
|---|---|---|
google_ads | Any *Service.mutate_* (create, update, remove) and the conversion uploads | Google Ads |
google_data_manager | Conversions (events) and audience members, to Google Ads, Analytics, Display & Video 360 and Campaign Manager 360 | Google Data Manager |
microsoft_ads | Any *Service.Add*, Update*, Delete*, and offline conversions | Microsoft Advertising |
meta_ads | Any object's create_* edge, api_update, api_delete, and Conversions API events | Meta Ads |
openai_ads, linkedin_ads, amazon_ads, apple_ads, restapi | Any endpoint of the API, as an HTTP request (below) | Connectors |
REST API connectors
A REST API connector's output sends HTTP requests instead of naming a service and method. Its operation: is a
request, like a call step's, with the connection's base URL, headers and sign-in:
outputs:
- name: send_events
connection: ads_api # a REST API connector, e.g. provider: linkedin_ads
step: events
operation:
path: /rest/conversionEvents # added to the connection's http.base_url
method: POST # POST (default), PUT, PATCH or DELETE
apply:
on_row_error: continue
| Key | What it does | Default |
|---|---|---|
path | The endpoint; {{ row.<column> }} puts a row's value in it, e.g. /campaigns/{{ row.id }} | Required |
method | POST, PUT, PATCH or DELETE | POST |
json | The body. {{ row }} is the whole row and {{ row.<column> }} one value; {{ rows }} sends the chunk's rows in one request | The row itself |
params, headers | The query string and extra headers; may use {{ row.<column> }} | None |
records | {path: ...}: the part of the response kept as each row's result | The whole response |
How rows are sent:
- One request per row (the default). A row the API rejects (a 4xx status) fails alone, with the status as its
error code. Use
chunk_sizeto set how many rows are sent between progress lines. - One request per chunk, when
jsonholds{{ rows }}, for examplejson: {elements: "{{ rows }}"}. Every row of the chunk gets the call's outcome. Withrecords: {path: results}, row n gets item n of that list. A single row's value ({{ row.<column> }}) cannot be used then. - Retries.
PUTandDELETEare idempotent, so they followpolicy.retry.POSTandPATCHare sent once; a request cut short keeps the rows sent before it and holds the rest (unknown outcomes). - The output's
policydefaults to the connection'spolicy(rate limit and retries).
Rejected rows
Rejected rows fail their batch under on_row_error: fail, above error_threshold, or by an error rule's then: fail. The scope's next run deals with that batch as runtime.state.failed_job_policy says
(handle a failed batch):
| Policy | What happens to the rejected rows |
|---|---|
abandon (default) | Nothing is sent again; the rejected rows stay failed. |
retry | The retry sends again only the batch's failed rows of those outputs (not the ones an error rule ignored), and the rows not sent yet. If they are rejected again, the batch fails again. The next run retries it while it has been started fewer than runtime.state.max_job_attempts times. |
block | The run stops until streamwright state retry --url URL JOB_ID or streamwright state abandon --url URL JOB_ID. A retry ignores the outputs' apply: settings, so it sends every failed row of the batch again. |
On any retry:
- applied rows are never sent again;
- rows in flight follow unknown outcomes;
- rows you skipped (
UNKNOWN_OUTCOME) stay failed; - a row keeps its attempts and last error until it is sent again.
Fix the cause first (the data, or the vendor account): a retry sends the same pinned rows with the same arguments.
Unknown outcomes
A call cut short leaves its rows in flight: nobody knows whether the vendor applied them. This happens when the run stops mid-call, or when a transient error outlasts the retries.
- An idempotent operation (one the vendor deduplicates, like conversion uploads) is sent again by the next run.
- Otherwise,
on_unknown: hold(the default) stops the run with exit 1 and names the batch. Check the vendor, then either:streamwright state resend --url URL JOB_ID: send them again on the next run (they may be applied twice), orstreamwright state skip --url URL JOB_ID: mark them failed withUNKNOWN_OUTCOME.
on_unknown: reconcileasks the connector what already exists, where it can.
A call of a non-idempotent operation is sent once: policy.retry does not apply to it. A call that timed out may
have been applied, so it is never resent within the run.
Logs and results
Run log
Each call logs its chunk and time on streamwright.output. The output ends with one applied: line with the
batch's totals:
output upload: call 1/3: 2,000 row(s), 1,998 applied, 2 failed, 1.4 s
applied: job_id=..., output=upload, rows=4998, failed=2, calls=3, duration_ms=4210
The run's last line and its summary count each apply output's rows: upload 4998 applied, 2 rejected. In the
--summary JSON, an apply output has applied and rejected (and rows, the same as applied).
Result log
Once a run has sent a batch's rows, the output's log: logs a line per row of the results, like a step's
log:. Each line is output NAME: MESSAGE on the streamwright.output
logger. It works the same on DuckDBJob and SparkJob, whatever the connector. Only apply outputs take log:.
| Key | What it does | Default |
|---|---|---|
sql | One SELECT over the results table (below) | Every row |
message | The line; {column} names a column of the SELECT | |
level | debug, info or warning | info |
max_rows | Lines to log, at most 1000; then one ... and N more row(s) line | 20 |
The results table is named after the output:
| Column | Holds |
|---|---|
| The output's columns | Each row as the output's SQL made it (gclid, campaign.name, ...) |
outcome.status | applied or failed |
outcome.error_code, outcome.error_message | Why a failed row failed: the vendor's code and message (for Google Ads, the field it names) |
outcome.result | JSON: what the vendor returned for an applied row (for example outcome.result->>'$.resource_name'), and any warnings the connector keeps |
outcome.row | The row's number among the output's rows in the batch, from 1 |
outcome.tag, outcome.then, outcome.error | For a failed row: its error rule's tag and then, and the vendor's other error fields (JSON) |
log: can also be a list. Each entry has its own sql, message, level and max_rows, and they run in turn.
For example, a count per error code, then the rows:
log:
- sql: |
SELECT outcome.error_code AS code, count(*) AS rows, min(gclid) AS example
FROM upload_conversions WHERE outcome.status = 'failed' GROUP BY 1
message: "{code}: {rows} conversion(s) rejected, e.g. gclid {example}"
level: warning
- sql: |
SELECT outcome.row AS row, gclid, outcome.error_code AS code, outcome.error_message AS error
FROM upload_conversions WHERE outcome.status = 'failed' AND outcome.then <> 'ignore'
ORDER BY outcome.row
message: "row {row}: gclid {gclid} rejected: {code}: {error}"
level: warning
max_rows: 50
Good to know about log:
{outcome.status}and{outcome.result.resource_name}reach into the struct without selecting it;{{ batch.id }}is the batch.- The SELECT runs in an in-memory DuckDB that holds only the results: no files, no network.
- The lines cover the rows this run sent; a row resent by a retry is logged again. They are redacted with the run's secrets and cut to 1,000 characters.
- An entry that cannot run is a warning, never a failure: the rows were already sent. The other entries still run.
- Under
streamwright serve(no row values in logs) each entry logs only how many rows its SELECT returned.
Record what was sent
An output whose sql reads an apply output's results is written once the batch's rows were sent. Use it to record a
feed as processed, in a table of your own that the next run's lookup skips:
- name: bookkeeping
connection: app_db # postgres, mysql or duckdb
table: processed_feeds
mode: merge
primary_key: [path, size, modified]
sql: |
SELECT '{{ config.feed_path }}' AS path, {{ config.feed_size }} AS size, '{{ config.feed_modified }}' AS modified,
{{ batch.id }} AS batch_id, now() AS processed_at,
count(*) FILTER (WHERE outcome.status = 'applied') AS applied,
count(*) FILTER (WHERE outcome.status = 'failed') AS rejected
FROM upload_conversions
- Every row of the batch. The results hold all the batch's rows with their last outcome, including the rows an earlier run sent.
- Results only. It reads apply outputs' results, config values and
{{ batch.id }}/{{ batch.ts }}, never a step: a retry or a later run sends the rows without building the steps.validaterefuses a step. - Quoting. Config values are pasted as text, so quote them.
{{ batch.id }}and{{ batch.ts }}are already quoted. - No rows sent. An aggregate without
GROUP BYstill writes one row, with counts of 0. WithGROUP BY, none. - A failed batch writes no row. Rejected rows that fail the batch, or a failed write of this output, fail the
batch, and
runtime.state.failed_job_policydecides. A retry sends only what is left (nothing, when only the write failed), then writes the row. - Job keys. A
job_keystops every later run with that key in its scope, even after its batch was abandoned unrecorded. Usefailed_job_policy: retryorblock(validatewarns aboutabandon). To run a key again on purpose:streamwright state forget-key --url URL --job JOB --scope SCOPE KEY. - Merge. With
mode: mergeand aprimary_key, a write repeated after a crash keeps one row. - DuckDB jobs only. A response may also read steps.
Good to know
- Row cap.
runtime.limits.apply_rows(default 100000) caps the rows of one output per batch, so a runaway join fails the batch before any call. - Validation checks the operation against the installed vendor library (a typo gets a "did you mean"), its
arguments,
apply.chunk_sizeagainst the operation's maximum, and every policy value. - Dry runs belong to the vendor, as an argument of its operation (Google Ads:
validate_only: truechecks every row and changes nothing). To offer one, declare a boolean config input and pass it on, asvalidate_onlyin the example does. The CLI stays vendor-neutral.
See also
- Input payloads: rows the caller hands to the run, a common source for apply outputs. Rows can also come from files a job reads (S3, a local folder), shaped with SQL.
- DuckDB jobs: runtime, state and failed batches.
- Responses: return each row's outcome to the caller, joined with the rows the job decided not to send.