# How can I define a Pydantic schema for heterogeneous Node-RED workflows when using OpenAI structured outputs?

**URL:** https://discourse.nodered.org/t/how-can-i-define-a-pydantic-schema-for-heterogeneous-node-red-workflows-when-using-openai-structured-outputs/99352
**Category:** General
**Created:** [11 October 2025 15:05 UTC](https://discourse.nodered.org/t/how-can-i-define-a-pydantic-schema-for-heterogeneous-node-red-workflows-when-using-openai-structured-outputs/99352 "2025-10-11T15:05:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ashfaq92](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ashfaq92](https://discourse.nodered.org/u/ashfaq92)
#### Post date: [11 October 2025 15:05 UTC](https://discourse.nodered.org/t/how-can-i-define-a-pydantic-schema-for-heterogeneous-node-red-workflows-when-using-openai-structured-outputs/99352/1 "2025-10-11T15:05:33Z")

</div>

Hi, I am new to Node-RED. I have a question where I needed help, I posted this question on [stackoverflow](https://stackoverflow.com/q/79788036), but here I paste again:

I’m experimenting with generating _Node-RED workflows_ using an LLM. As expected, the unstructured JSON responses are often messy or invalid.

To improve reliability, I’m trying to use OpenAI’s `structured_output` feature. It works nicely with simple, fixed schemas — for example:

```auto
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI()

class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]

response = client.responses.parse(
    model="gpt-4o-2024-08-06",
    input=[
        {"role": "system", "content": "Extract the event information."},
        {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
    ],
    text_format=CalendarEvent,
)

event = response.output_parsed

```

However, in my case, the target structure — a _Node-RED flow_ — is much more complex and heterogeneous.

A Node-RED flow (or subflow) can contain many types of nodes, each with its own attributes. The [Node-RED admin API docs](https://nodered.org/docs/api/admin/types) describe the general structure, e.g.:

```auto
{
  "id": "1234",
  "label": "Sheet1",
  "nodes": [...],
  "configs": [...],
  "subflows": [...]
}

```

My question is:  
_How should I define a Pydantic model (or a hierarchy of models) that can represent this flexible workflow structure, so that an LLM’s structured output can conform to it?_

I understand that each `node` type may have its own schema, but I’m not sure how to model this polymorphism in a way that still works well with OpenAI’s `structured_output` or `responses.parse`.

Any examples or design patterns for handling this kind of heterogeneous JSON structure would be appreciated.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [9 January 2026 15:06 UTC](https://discourse.nodered.org/t/how-can-i-define-a-pydantic-schema-for-heterogeneous-node-red-workflows-when-using-openai-structured-outputs/99352/2 "2026-01-09T15:06:02Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
