Skip to content
providerkit
GitHub

stripLeakedToolMarkup

function stripLeakedToolMarkup(chunks, opts?): AsyncGenerator<ProviderChunk>;

Defined in: core/src/tool-markup.ts:82

Strip tool calls a model wrote into its text, as the stream arrives.

for await (const chunk of stripLeakedToolMarkup(provider.createStream(messages, tools))) {
if (chunk.content) write(chunk.content);
}

A tag can arrive split across chunks, so text that could still become one (<, <suggest_) is held until the next chunk settles it. Held text goes out before the finish chunk, and before the stream’s own error. Only content changes: every chunk still passes, in order, with its reasoning, tool calls and usage. A citation whose span reaches text that was removed, or is still held, keeps its source and loses its span, as any span that doesn’t check out against the text does.

A turn that was nothing but markup comes out with no text. Wrap the result in requireContent to have it fail like any other empty turn.

onStrip runs once for each leak removed, with the markup that set it off: a whole tag, or a DeepSeek token and the rest of the text it arrived in. The rest of a leak goes without another call: a pair’s closing tag, and text after a DeepSeek token in later chunks.

ponytail: a leaked call is dropped, not turned into the tool call it meant. Some get through, whole or in part: a pair of tags with no attributes (GLM’s call format) stays whole, the text between a pair’s tags stays, a > inside an attribute ends the tag early, and a tag split across chunks that runs past 512 characters before it closes leaks. Code is not exempt: a snake_case tag with an attribute, as in an XML sample, is removed from a code block too. Count onStrip to learn whether recovering the calls is worth building.

Parameter Type
chunks AsyncIterable<ProviderChunk>
opts { onStrip?: (markup) => void; }
opts.onStrip? (markup) => void

AsyncGenerator<ProviderChunk>