tagged_llm_text_parser
Parse tagged thoughts, such as <think>...</think>, out of streamed LLM text.
Some models write their reasoning into the answer text, wrapped in tags, ahead
of the answer itself. TaggedLLMTextParser separates that reasoning
from the text that follows so a service can emit it as thoughts rather than as
text to speak.
- class pipecat.utils.text.tagged_llm_text_parser.TaggedLLMTextParsedOutputChunkKind(*values)[source]
Bases:
EnumThe kind of a
TaggedLLMTextParsedOutputChunk.- Parameters:
TEXT – Text outside the thought.
THOUGHT_START – The thought began. Its text is empty.
THOUGHT_TEXT – Text inside the thought.
THOUGHT_END – The thought ended. Its text is empty.
- TEXT = 'text'
- THOUGHT_START = 'thought_start'
- THOUGHT_TEXT = 'thought_text'
- THOUGHT_END = 'thought_end'
- class pipecat.utils.text.tagged_llm_text_parser.TaggedLLMTextParsedOutputChunk(kind: TaggedLLMTextParsedOutputChunkKind, text: str = '')[source]
Bases:
objectA categorized piece of text produced by
TaggedLLMTextParser.- Parameters:
kind – What the piece is.
text – The piece’s text; empty for
THOUGHT_STARTandTHOUGHT_END.
- class pipecat.utils.text.tagged_llm_text_parser.TaggedLLMTextParser(thought_open_tag: str, thought_close_tag: str)[source]
Bases:
objectParse a tagged thought out of streamed LLM text.
Feed the text chunk by chunk to
parse_next_chunk(), which returns the categorized pieces each chunk completes: answer text, or a thought’s start, text and end.A thought is recognized only at the start of the text, optionally after whitespace; tags later in the text are passed on as text. Tags may be split across chunks, so the start of the text is held back until it is clear whether it opens a thought, and inside a thought, a possible start of the closing tag is held back. Text that doesn’t open with a thought passes through from the first chunk that rules one out, and whitespace between a thought and the text after it is dropped.
Call
flush()when the text ends, orreset()when it is cut short. Either readies the parser for the next text.Example:
parser = TaggedLLMTextParser("<think>", "</think>") for text in ["<thi", "nk>Hmm.</th", "ink> ", "Hello!"]: for chunk in parser.parse_next_chunk(text): ... for chunk in parser.flush(): ...
- __init__(thought_open_tag: str, thought_close_tag: str)[source]
Initialize the parser.
- Parameters:
thought_open_tag – The tag that opens a thought, e.g.
"<think>".thought_close_tag – The tag that closes a thought, e.g.
"</think>".
- parse_next_chunk(text: str) list[TaggedLLMTextParsedOutputChunk][source]
Parse the next chunk of streamed text.
- Parameters:
text – The next chunk of streamed text.
- Returns:
The pieces this chunk completes, in order.
- flush() list[TaggedLLMTextParsedOutputChunk][source]
End the text, releasing what was held back and closing an open thought.
- Returns:
The remaining pieces, in order.
- reset() list[TaggedLLMTextParsedOutputChunk][source]
End text that was cut short, e.g. by an interruption or an error.
Text still held back is discarded.
- Returns:
A
THOUGHT_ENDpiece if a thought was open, so the caller can close it; otherwise nothing.