# stream_transform

URL: /kida/api/compiler/stream_transform/
Section: compiler
Description: Sync-to-stream Python AST transformer.

Transforms compiled sync block stmts (_append calls) into streaming stmts
(yield expressions). This eliminates a redundant full Kida AST compilation
per block by deriving the stream variant from the sync compilation output.

The transform replaces:
    _append(expr)  →  yield expr
    return ''.join(buf)  →  (removed)

The preamble and return/sentinel are handled by the caller, not this transform.

Uses copy-on-write: only nodes on the path to ``_append`` call sites are
copied.  Unchanged subtrees are shared with the original AST, making this
safe to call multiple times on the same input (e.g. for stream + async
stream variants).

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/compiler/stream_transform/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstream_transform%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstream_transform%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstream_transform%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstream_transform%2Findex.txt)

Module

#
`compiler.stream_transform`

Sync-to-stream Python AST transformer.

Transforms compiled sync block stmts (_append calls) into streaming stmts
(yield expressions). This eliminates a redundant full Kida AST compilation
per block by deriving the stream variant from the sync compilation output.

The transform replaces:

```
_append(expr)  →  yield expr
return ''.join(buf)  →  (removed)
```

The preamble and return/sentinel are handled by the caller, not this transform.

Uses copy-on-write: only nodes on the path to`_append`call sites are
copied. Unchanged subtrees are shared with the original AST, making this
safe to call multiple times on the same input (e.g. for stream + async
stream variants).

1Class1Function

## Classes

`_AppendToYield`

2

▼

Replace ``_append(expr)`` with ``yield expr`` in Python AST.

Overrides ``generic_visit`` with a co…

Replace`_append(expr)` with `yield expr`in Python AST.

Overrides`generic_visit`with a copy-on-write strategy: only nodes
whose children actually changed are shallow-copied. Unchanged subtrees
are returned as-is (identity), so the original AST is never mutated.

#### Methods

`visit_Expr`

1

`ast.Expr`

▼

Transform _append(x) expression statements to yield x.

`def visit_Expr(self, node: ast.Expr) -> ast.Expr`

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`ast.Expr`

`generic_visit`

1

`ast.AST`

▼

Copy-on-write: only copy nodes whose children changed.

`def generic_visit(self, node: ast.AST) -> ast.AST`

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`ast.AST`

## Functions

`sync_body_to_stream`

1

`list[ast.stmt]`

▼

Transform compiled sync body statements into streaming equivalents.

Walks the …

`def sync_body_to_stream(stmts: list[ast.stmt]) -> list[ast.stmt]`

Transform compiled sync body statements into streaming equivalents.

Walks the AST and replaces`_append(expr)` calls with `yield expr`.
Uses copy-on-write so the original stmts are never modified — safe to
call repeatedly on the same input.

##### Parameters

Name
Type
Description

`stmts`
`list[ast.stmt]`

Compiled Python AST statements from sync block compilation.

##### Returns

`list[ast.stmt]`
