Skip to content

Output Transformer

Modules concerning the output transformers used in Gen AI applications.

IdentityOutputTransformer(event_emitter=None)

Bases: BaseOutputTransformer

An output transformer that transforms the output of a language model into an identity function.

This transformer simply returns the output and stream events of the language model as is.

JSONOutputTransformer(event_emitter=None)

Bases: IdentityOutputTransformer

An output transformer that parses the JSON string in the language model output into a Python dictionary.

This output transformer is useful to parse a structured output from the output of a language model that doesn't support structured output natively. It only supports parsing a single JSON object from the text output. Text outputs that contain multiple JSON objects will be returned as the original text output.

This output transformer will not perform any transformation for streaming events.

Attributes:

Name Type Description
event_emitter EventEmitter | None

The event emitter to use for streaming events.

Examples:

LMOutput transformation: Input: python LMOutput(outputs=[ LMOutputItem(type="text", output='Here is the output: {"key": "value"}'), ])

Output:
```python
LMOutput(outputs=[
    LMOutputItem(type="structured", output={"key": "value"}),
])
```

ThinkTagOutputTransformer(event_emitter=None)

Bases: BaseOutputTransformer

An output transformer that handles special thinking tags used in certain open source language models.

Some open source language models, such as Qwen3-30B-A3B, emits their thinking tokens as part of the text tokens, These thinking tokens are wrapped in special XML-like thinking tags to indicate the start and end of thinking. This transformer detects and separates these thinking tags from the text output to handle the thinking properly.

Attributes:

Name Type Description
event_emitter EventEmitter | None

The event emitter to use for streaming events.

thinking bool

Whether the transformer is currently in thinking mode.

thinking_id str

The ID of the current thinking.

Examples:

LMOutput transformation: Input: python LMOutput(outputs=[ LMOutputItem(type="text", output="<think>I'm thinking...</think>I'm responding..."), ])

Output:
```python
LMOutput(outputs=[
    LMOutputItem(type="thinking", output=Reasoning(reasoning="I'm thinking...")),
    LMOutputItem(type="text", output="I'm responding..."),
])
```

Streaming event transformation: Input: python Event(id=None, type="response", value="<think>") Event(id=None, type="response", value="I'm thinking...") Event(id=None, type="response", value="leading text</think>trailing text") Event(id=None, type="response", value="I'm responding...")

Output:
```python
Event(id=None, type="thinking_start", value="<think>")
Event(id=None, type="thinking", value="I'm thinking...")
Event(id=None, type="thinking", value="leading text")
Event(id=None, type="thinking_end", value="</think>")
Event(id=None, type="response", value="trailing text")
Event(id=None, type="response", value="I'm responding...")
```

Initializes a new instance of the ThinkTagOutputTransformer class.

Parameters:

Name Type Description Default
event_emitter EventEmitter | None

The event emitter to use for streaming events. Defaults to None.

None