Skip to content

Commit 1df904d

Browse files
committed
Add apply_add() method to docs
1 parent 96d1b7c commit 1df904d

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

docs/client.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ Parameters mirror `generate(...)`:
227227

228228
Each `PatchEvent` carries:
229229

230-
- `event.op` — the raw RFC 6902 operation (`{"op": "add", "path": ..., "value": ...}`)
230+
- `event.op` — the raw RFC 6902 operation (`{"op": "add", "path": ..., "value": ...}`).
231+
The dottxt API only ever emits `add` ops; `dottxt.apply_add(doc, path, value)`
232+
folds one into a object in place, returning the (possibly new) root.
231233
- `event.snapshot` — an independent deep copy of the JSON object built up to
232234
and including this op
233235
- `event.is_leaf` / `event.field` / `event.value` convenience demux for
@@ -274,6 +276,26 @@ milliseconds in while `reply` continues to stream. If you need the full
274276
object so far (e.g. to log progress or hand a partial object to another
275277
service), use `event.snapshot`.
276278

279+
To drive your own state from the raw ops instead of the snapshot (e.g. to
280+
mirror the object into a store of your own) fold each `event.op` in with
281+
`apply_add`:
282+
283+
```python
284+
from dottxt import AsyncDotTxt, apply_add
285+
286+
async def main():
287+
client = AsyncDotTxt()
288+
doc = {} # the stream's first op is the root seed
289+
stream = client.stream(
290+
model="openai/gpt-oss-20b",
291+
response_format=SupportTicket,
292+
input="I was charged twice this month, please refund the duplicate.",
293+
)
294+
async for event in stream:
295+
doc = apply_add(doc, event.op["path"], event.op["value"])
296+
print(doc)
297+
```
298+
277299
Errors:
278300

279301
- `dottxt.PatchStreamError`: raised when the gateway returns a non-200

0 commit comments

Comments
 (0)