โšก Technical Documentation

Structured Outputs In Openwebui: A Comprehensive Guide

Technical guide covering structured outputs in openwebui: a comprehensive guide

๐Ÿ‘ค
Author
Cosmic Lounge AI Team
๐Ÿ“…
Updated
6/1/2025
โฑ๏ธ
Read Time
19 min
Topics
#llm #ai #model #fine-tuning #training #api #code #programming #introduction #design

๐Ÿ“– Reading Mode

๐Ÿ“– Table of Contents

๐ŸŒŒ Structured Outputs in OpenWebUI: A Comprehensive Guide

OpenWebUI has emerged as a leading open-source platform for interacting with large language models (LLMs) 1. One of its most powerful features is the ability to generate structured outputs, enabling developers to define precise formats for LLM responses. This article delves deep into the intricacies of structured outputs in OpenWebUI, focusing on the utilization of tools and filters to achieve this functionality.



๐ŸŒŸ What are Structured Outputs?

Traditionally, LLMs produce unstructured text, which can be challenging to integrate into applications and workflows. Structured outputs address this by allowing developers to specify a schema or format that dictates how the LLM should organize its response. This ensures that the output is predictable, machine-readable, and readily usable for various purposes. This approach bridges the gap between powerful AI insights and actionable business results 2. Instead of struggling with unorganized text, structured outputs ensure that LLM responses adhere to a pre-defined, machine-readable format, typically using JSON. Structured outputs guarantee that the LLMโ€™s response will conform to the chosen structure, enhancing reliability for applications 3. For example, if you need to extract specific data points from a text, you can define a schema that specifies the exact format for those data points. The LLM will then adhere to this schema, ensuring that the extracted data is consistent and readily usable. Some common use cases for structured outputs include:

  • Populating databases: Extracting information from text and storing it in a structured format 2.

  • Generating code: Creating code snippets in a specific programming language based on user instructions 2.

  • Creating user interfaces: Translating visual designs into functional code by defining a schema that includes HTML tags, CSS classes, and content structures 2.

  • Extracting entities for function calling: Identifying and extracting key entities from user input to trigger specific actions or functions 4.



๐ŸŒŸ Tools and Filters in OpenWebUI

OpenWebUI offers a versatile plugin system with tools and filters that empower developers to manipulate and structure LLM outputs effectively. These components work together to form pipelines, which are essentially sequences of operations that process data flowing between the user and the LLM 5. There are four main types of pipeline objects:

  • Filters: These modify incoming user messages and outgoing LLM responses. They can be used for tasks like sanitizing input, adding context, or formatting output.

  • Pipes: These are functions that perform actions before returning LLM messages to the user. They can be used for more complex tasks like Retrieval Augmented Generation (RAG) or executing custom code.

  • Tools: These are simple functions that the LLM can decide to call or not. They are useful for providing the LLM with access to external resources or specialized capabilities.

  • Valves: These are configurable options that allow users to adjust the behavior of filters. For example, for a filter that converts text to uppercase, a valve could control whether the entire output is capitalized or just the first letter of each sentence.

โšก Filters

Filters act as middleware, modifying data before it reaches the LLM (input) or after itโ€™s generated (output) 6. They are particularly useful for lightweight, real-time alterations to the data flow.

  • Inlet Function: This function pre-processes user input before itโ€™s sent to the LLM. It can be used to:
  • Add context to ambiguous queries
  • Format data into JSON or Markdown
  • Sanitize input by removing unwanted characters
  • Streamline user input by injecting clarifying instructions
  • Outlet Function: This function post-processes the LLMโ€™s response before presenting it to the user. It can be used to:
  • Refine or format the output
  • Log changes for debugging or analytics
  • Redact sensitive information

โšก Tools

Tools are Python functions that the LLM can call upon to perform specific actions 5. Unlike filters, which are always executed, tools are invoked only when the LLM deems them necessary for fulfilling a request. This allows for more dynamic and context-aware interactions.



๐ŸŒŸ Defining and Using Tools and Filters

OpenWebUI provides a framework for defining and integrating custom tools and filters. Hereโ€™s a basic example of a filter that converts LLM responses to uppercase:

Python

from pydantic import BaseModel from typing import Optional

class Filter: class Valves(BaseModel): TRANSFORM_UPPERCASE: bool = True

def __init__(self): pass

def inlet(self, body: dict, __user__: Optional = None) -> dict: return body

def outlet(self, body: dict, __user__: Optional = None) -> dict: if self. Valves. TRANSFORM_UPPERCASE: for message in body[โ€œmessagesโ€]: if message[โ€œroleโ€] == โ€œassistantโ€: message[โ€œcontentโ€] = message[โ€œcontentโ€].upper() return body

This filter defines a valve TRANSFORM_UPPERCASE that allows users to enable or disable the uppercase transformation. The inlet function remains passive, while the outlet function modifies the LLM output based on the valve setting.



๐ŸŒŸ Examples of Structured Outputs

Here are some practical examples of how structured outputs can be used in OpenWebUI:

  • Math Tutor: A math tutoring tool that outputs steps to solving a math problem as an array of structured objects 4. This allows the user to progress through the solution at their own pace.

  • Text Summarization: Summarizing articles following a specific schema, such as extracting key entities, dates, and topics 2. This can be used to populate a database or display information in a structured format.



๐ŸŒŸ Types of Structured Outputs

OpenWebUI supports various types of structured outputs, including:

  • JSON: A widely used format for representing data objects.

  • XML: A markup language for structuring data in a hierarchical format.

  • CSV: A simple format for storing tabular data.

  • Custom Formats: Developers can define their own formats using tools and filters.



๐ŸŒŸ Limitations and Constraints

While structured outputs offer significant advantages, there are some limitations to consider:

  • Model Capabilities: Not all LLMs are equally proficient at generating structured outputs. Some models may require fine-tuning or specific prompting techniques to achieve optimal results. For example, older models might struggle with complex schemas or produce outputs that donโ€™t strictly adhere to the defined format.

  • Latency: Generating structured outputs can sometimes introduce latency, especially for complex schemas or large responses. This can be particularly problematic for real-time applications where responsiveness is crucial.

  • Error Handling: Robust error handling is crucial to ensure that applications can gracefully handle unexpected outputs or refusals from the LLM. For instance, the API response includes a refusal field that indicates when the model refuses to answer due to safety reasons 4. This allows developers to handle such situations appropriately and provide informative feedback to the user.



๐ŸŒŸ Best Practices

Here are some best practices for using structured outputs in OpenWebUI:

  • Clear Instructions: Provide the LLM with clear and concise instructions on the desired output format. Use delimiters like triple quotes or XML tags to clearly indicate distinct parts of the input 7.

  • Schema Validation: Validate the LLMโ€™s response against the defined schema to ensure data integrity. This helps identify any inconsistencies or errors in the output.

  • Iterative Refinement: Start with simple schemas and gradually increase complexity as needed. This allows you to test and refine the process incrementally.

  • Monitoring and Logging: Monitor LLM responses and log any errors or unexpected outputs for debugging and improvement.

  • Specify Steps: For complex tasks, break down the process into a sequence of steps and provide these steps explicitly to the LLM 7.

  • Provide Examples: In some cases, it might be helpful to provide the LLM with examples of the desired output format 7.



๐ŸŒŸ Troubleshooting

If you encounter issues with structured outputs, here are some troubleshooting steps:

IssueSolution
Model not generating structured outputVerify that the chosen LLM supports structured outputs and is configured correctly.
Output not matching the schemaReview the schema definition for any errors or inconsistencies.
Errors in custom tools or filtersDebug the code to identify and fix any issues.
Unexpected behavior or errorsConsult the OpenWebUI documentation for troubleshooting tips and common solutions.


๐ŸŒŸ Advanced Techniques

OpenWebUI allows for advanced techniques to further enhance structured outputs:

  • Combining Tools and Filters: Create complex workflows by chaining multiple tools and filters together. For example, you could use a filter to extract entities from user input and then use a tool to query a database based on those entities.

  • Custom Tools: Develop custom tools to perform specialized actions or integrate with external systems. This allows you to extend the capabilities of OpenWebUI and tailor it to your specific needs.

  • Fine-tuning: Fine-tune LLMs to improve their performance in generating specific types of structured outputs 8. This can involve training the model on a dataset of structured data or adjusting hyperparameters to optimize for a particular schema.

  • Asynchronous Chat Support: OpenWebUI supports asynchronous chat, which is particularly beneficial for long-running tasks or multi-agent workflows 9. This allows the LLM to process requests in the background while the user continues to interact with the interface.

  • Creative Applications: Structured outputs can be used for a wide range of creative applications, such as generating visualizations from table data or building interactive chatbots with complex dialogue flows. For example, a dedicated mermaid bot could automatically generate diagrams from tabular data in chat messages 9.

  • URL Prompt Generation: Advanced techniques can be used to generate intricate and well-structured URL prompts for platforms like WebSim.ai 10. This involves creatively utilizing various URL components, such as domain names, paths, query parameters, and fragments, to encode complex information and instructions.



๐ŸŒŸ Conclusion

Structured outputs are a powerful feature in OpenWebUI, enabling developers to unlock the full potential of LLMs for various applications. By leveraging tools and filters, developers can effectively define and manipulate LLM responses to achieve precise and machine-readable outputs. The ability to structure LLM outputs opens up a world of possibilities, from automating complex tasks to creating innovative user interfaces and interactive experiences.

๐Ÿ”ง Works cited

1. What is Open WebUI? The best self hosted, open source ChatGPT alternative?, accessed on January 30, 2025, https://www.pondhouse-data.com/blog/introduction-to-open-web-ui

2. The Missing Link in AI: Why Structured Outputs are the Next Big Thing | by Kelvin Campelo, accessed on January 30, 2025, https://medium.com/@kelvincampelo/get-organized-results-with-ai-how-structured-outputs-are-changing-the-game-9dd2e6449bd1

3. Ollama now supports structured outputs - Reddit, accessed on January 30, 2025, https://www.reddit.com/r/ollama/comments/1h8a2or/ollama_now_supports_structured_outputs/

4. Introduction to Structured Outputs | OpenAI Cookbook, accessed on January 30, 2025, https://cookbook.openai.com/examples/structured_outputs_intro

5. Features | Open WebUI, accessed on January 30, 2025, https://docs.openwebui.com/features/

6. Filter Function: Modify Inputs and Outputs - Open WebUI, accessed on January 30, 2025, https://docs.openwebui.com/features/plugin/functions/filter/

7. Prompt engineer this Prompt | Open WebUI Community, accessed on January 30, 2025, https://openwebui.com/p/sephah/prompt-engineer-this

8. Open WebUI Fine Tuning Techniques - Restack, accessed on January 30, 2025, https://www.restack.io/p/fine-tuning-answer-open-webui-cat-ai

9. Open WebUI v0.5.0 (Asynchronous Chats, Channels, Structured Output, Screen Capture and more) : r/OpenWebUI - Reddit, accessed on January 30, 2025, https://www.reddit.com/r/OpenWebUI/comments/1hm817u/open_webui_v050_asynchronous_chats_channels/

10. Websim Prompt Generator Prompt | Open WebUI Community, accessed on January 30, 2025, https://www.openwebui.com/p/sramelyk/websim-prompt-generator