Output Transformer
Modules concerning the output transformers used in Gen AI applications.
EventFilterOutputTransformer(event_types)
Bases: IdentityOutputTransformer
An output transformer that filters stream events by type.
This transformer lets users define the base event types to keep (e.g. thinking).
At runtime, only events matching the selected types are kept, including their
corresponding _start and _end events.
Attributes:
| Name | Type | Description |
|---|---|---|
allowed_event_types |
set[str]
|
The set of allowed event types. |
Initializes a new EventFilterOutputTransformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_types
|
list[str]
|
Base event types to keep. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any event type is empty. |
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"}),
])
```
OutputTransformerChain(transformers, event_emitter=None)
A wrapper to chain a list of output transformers.
The OutputTransformerChain class defines the interface for transforming
the output of language models using a chain of output transformers.
Attributes:
| Name | Type | Description |
|---|---|---|
transformers |
list[BaseOutputTransformer]
|
The list of output transformers to use. |
event_emitter |
EventEmitter | None
|
The event emitter to use for streaming events. |
Initializes a new instance of the OutputTransformerChain class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transformers
|
list[BaseOutputTransformer]
|
The list of output transformers to use. |
required |
event_emitter
|
EventEmitter | None
|
The event emitter to use for streaming events. Defaults to None. |
None
|
emit(event)
async
Transforms a stream event of a language model and emits it using the event emitter.
This method transforms a stream event of a language model using the chain of output transformers and emits it using the event emitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
The stream event to transform and emit. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the event emitter is not set. |
from_configs(configs, event_emitter=None)
classmethod
Create an output transformer chain from a list of output transformer configs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configs
|
list[OutputTransformerConfig]
|
The list of output transformer configs to use. |
required |
event_emitter
|
EventEmitter | None
|
The event emitter to use for streaming events. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerChain |
OutputTransformerChain
|
The output transformer chain. |
transform(output)
Transforms the output of a language model.
This method transforms the output using the chain of output transformers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
LMOutput
|
The output to transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LMOutput |
LMOutput
|
The transformed output of a language model. |
OutputTransformerConfig
Bases: BaseModel
Defines the output transformer config schema.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
OutputTransformerType
|
The type of output transformer to use. |
kwargs |
dict[str, Any]
|
The additional keyword arguments for the output transformer. |
build()
Build an output transformer from the config.
Returns:
| Name | Type | Description |
|---|---|---|
BaseOutputTransformer |
BaseOutputTransformer
|
The output transformer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the output transformer type is invalid. |
event_filter(event_types)
classmethod
Create an event filter output transformer config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_types
|
list[str]
|
Base event types to keep. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerConfig |
'OutputTransformerConfig'
|
The event filter output transformer config. |
from_config(config)
classmethod
Create an output transformer config from a string or a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
OutputTransformer
|
The output transformer config, can be one of the following: 1. str: The type of the output transformer 2. dict[str, Any]: The output transformer config as a dictionary 3. OutputTransformerConfig: The output transformer config itself |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerConfig |
'OutputTransformerConfig'
|
A new output transformer config. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
identity()
classmethod
Create an identity output transformer config.
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerConfig |
'OutputTransformerConfig'
|
The identity output transformer config. |
json()
classmethod
Create a JSON output transformer config.
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerConfig |
'OutputTransformerConfig'
|
The JSON output transformer config. |
think_tag()
classmethod
Create a think tag output transformer config.
Returns:
| Name | Type | Description |
|---|---|---|
OutputTransformerConfig |
'OutputTransformerConfig'
|
The think tag output transformer config. |
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. |
response_id |
str
|
The ID of the current response. |
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=Thinking(thinking="I'm thinking...")),
LMOutputItem(type="text", output="I'm responding..."),
])
```
Streaming event transformation:
Input:
python
Event(id="original_id", type="response", value="<think>")
Event(id="original_id", type="response", value="I'm thinking...")
Event(id="original_id", type="response", value="leading text</think>trailing text")
Event(id="original_id", type="response", value="I'm responding...")
Output:
```python
Event(id="thinking_id", type="thinking_start", value="")
Event(id="thinking_id", type="thinking", value="I'm thinking...")
Event(id="thinking_id", type="thinking", value="leading text")
Event(id="thinking_id", type="thinking_end", value="")
Event(id="response_id", type="response_start", value="")
Event(id="response_id", type="response", value="trailing text")
Event(id="response_id", type="response", value="I'm responding...")
Event(id="response_id", type="response_end", value="")
```
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
|