# Output Transformer

[**`gllm-inference`**](https://github.com/GDP-ADMIN/gl-sdk/tree/main/libs/gllm-inference/gllm_inference/catalog) | **Tutorial**: [output-transformer](https://gdplabs.gitbook.io/sdk/gen-ai-sdk/tutorials/inference/lm-invoker/output-transformer "mention") | [API Reference](https://api.python.docs.gdplabs.id/gen-ai/library/gllm_inference/api/lm_invoker.html)

**Supported by:** All LM invokers

## What is Output Transformer?

**Output transformers** allow you to transform the raw output from the language model into a different format or structure. This is useful when you want to post-process the model's output before returning it to your application.

The LM Invoker supports output transformation through the `output_transformer` parameter, which can be configured during initialization. By default, LM invokers use the `IdentityOutputTransformer`, which returns the output as is.

Let's take a look at the available output transformer options!

## Identity Output Transformer

This output transformer performs identity transformation, meaning that it essentially performs no transformation to the output. This is the **default output transformer** for all LM invokers.

**Example:**

```python
import asyncio
from gllm_inference.lm_invoker import OpenAILMInvoker
from gllm_inference.model import OpenAILM
from gllm_inference.schema import OutputTransformerType

lm_invoker = OpenAILMInvoker(
    OpenAILM.GPT_5_NANO, 
    output_transformer=OutputTransformerType.IDENTITY,  # Optional, as it is the default value
)
```

## JSON Output Transformer

This output transformer automatically parses JSON strings contained in text objects into structured outputs. This is useful for language models that don't naturally support structured output.

{% hint style="warning" %}
JSON output transformer can only extract a single JSON object from a single text output item.
{% endhint %}

When utilizing this output transformer, we can instruct the model to output a structured JSON:

```python
import asyncio
from gllm_inference.lm_invoker import OpenAILMInvoker
from gllm_inference.model import OpenAILM
from gllm_inference.schema import OutputTransformerType

lm_invoker = OpenAILMInvoker(
    OpenAILM.GPT_5_NANO,
    output_transformer=OutputTransformerType.JSON
)

query = """Return a JSON object with keys 'name' and 'age' for a person named John who is 30 years old!"""
output = asyncio.run(lm_invoker.invoke(query))
print(output)
```

**Output:**

<pre class="language-python"><code class="lang-python"><strong># Without JSON output transformer (Raw output)
</strong>LMOutput(
    outputs=[
        LMOutputItem(type='text', output='Sure, here you go:\n{"name": "John", "age": 30}')
    ],
)

<strong># With JSON output transformer
</strong>LMOutput(
    outputs=[
        LMOutputItem(type='structured', output={'name': 'John', 'age': 30})
    ],
)
</code></pre>

## Think Tag Output Transformer

Some open source models, such as DeepSeek R1, embed their thinking output as part of the text output by separating them using the `<think>...</think>` special tags. To extract these thinking output gracefully, we can use the think tag output transformer.

This output transformer automatically handles these embedded thinking token both in the `LMOutput` object as well as in the streaming events. Feel free to utilize it the next time you try an open source model that utilize this kind of formatting to output their thinking!

Here's an example:

```python
import os
import asyncio
from gllm_inference.lm_invoker import OpenAIChatCompletionsLMInvoker
from gllm_inference.schema import OutputTransformerType

lm_invoker = OpenAIChatCompletionsLMInvoker(
    model_name="deepseek-ai/DeepSeek-R1",
    base_url="https://api.deepinfra.com/v1",
    api_key=os.getenv("DEEPINFRA_API_KEY"),
    output_transformer=OutputTransformerType.THINK_TAG,
    output_analytics=True,
)

query = """Solve this equation: 2x + 3 = 11"""
output = asyncio.run(lm_invoker.invoke(query))
print(output)
```

**Output:**

<pre class="language-python"><code class="lang-python"><strong># Without think tag output transformer (Raw output)
</strong>LMOutput(
    outputs=[
        LMOutputItem(type='text', output="""
            &#x3C;think>\nI have this equation: 2x + 3 = 11. I need to solve for x. 
            Solving means ...I think I\'m good.\n&#x3C;/think>\n
            To solve the equation...**Solution**: \\(x = 4\\)
        """)
    ],
)

<strong># With think tag output transformer
</strong>LMOutput(
    outputs=[
        LMOutputItem(type='thinking', output=Reasoning(
            reasoning="""
                I have this equation: 2x + 3 = 11. I need to solve for x. 
                Solving means ...I think I\'m good.\n
            """, ...)
        ),
        LMOutputItem(type='text', output="""
            \nTo solve the equation...**Solution**: \\(x = 4\\)
        """),
    ],
)
</code></pre>
