OpenAI Structured Chat

Chat with OpenAI enforcing structured JSON outputs using function calling.

Overview

This node enables structured chat interactions with OpenAI models that support function calling, such as GPT-4 and GPT-3.5-turbo-1106. It sends a user-defined prompt to the model along with a JSON schema describing the expected output structure. The node enforces that the response from OpenAI conforms to this schema by leveraging the function calling feature. This is particularly useful when you want predictable, machine-readable outputs from the AI, for example, generating structured data like JSON objects, configurations, or specific formatted responses.

Common scenarios include:

  • Extracting structured information from natural language prompts.
  • Automating data formatting tasks where the output must follow a strict schema.
  • Integrating AI-generated data into workflows that require validated JSON structures.

Example: You provide a prompt asking for a summary of a meeting and a JSON schema defining fields like date, participants, and summary. The node returns a JSON object matching this schema.

Properties

Name Meaning
Prompt The user prompt text sent to OpenAI to generate a response.
JSON Function Schema A JSON object defining the expected structure of the output. This schema guides the AI's response format.
Model The OpenAI model to use. Options are:
- gpt-4
- gpt-3.5-turbo-1106 (both support function calling)
Reasoning Effort Level of reasoning effort for the AI. Options are:
- low
- medium

Output

The node outputs an array of items, each containing a json field. This json field holds the parsed JSON object returned by OpenAI, which strictly follows the provided JSON function schema. The output is guaranteed to be structured according to the schema, enabling downstream nodes to process it reliably.

No binary data output is produced by this node.

Dependencies

  • Requires an API key credential for OpenAI to authenticate requests.
  • Uses the OpenAI SDK internally to communicate with the OpenAI API.
  • The selected model must support function calling (e.g., GPT-4 or GPT-3.5-turbo-1106).
  • No additional environment variables beyond the API key credential are needed.

Troubleshooting

  • No valid response from OpenAI: This error occurs if the node does not receive a completed response matching the schema. Possible causes:

    • The prompt or JSON schema is malformed or incompatible.
    • The selected model does not support function calling properly.
    • Network or API issues preventing a successful response.

    Resolution: Verify the prompt and JSON schema correctness, ensure the model supports function calling, and check your API key and network connectivity.

  • JSON parsing errors: If the output cannot be parsed as JSON, it may indicate the AI did not follow the schema or returned invalid JSON. Adjust the schema or prompt to improve compliance.

  • API quota or authentication errors: Ensure your API key is valid and has sufficient quota.

Links and References

Discussion