DEV Community

Tsubasa Kanno
Tsubasa Kanno

Posted on

Amazing Structured Outputs with Snowflake's COMPLETE Function

Introduction

On February 11, 2025, Cortex COMPLETE Structured Outputs became available as a public preview within Snowflake's Cortex AI functionality.

I initially skimmed through this feature announcement without much thought, but after trying it, I was amazed at how useful it is! Let me introduce it to you.

⚠️ Note (2025/2/25): Cortex COMPLETE Structured Outputs is currently in public preview. Features may be significantly updated in the future.

📝 Note: This article represents my personal views and not those of Snowflake.

What is Cortex COMPLETE Structured Outputs?

Cortex COMPLETE Structured Outputs is a feature that allows you to obtain responses from LLMs in a structured format that complies with a specified JSON schema. Normally, LLM outputs are returned in text format, requiring post-processing to be used as structured data. With this feature, you can skip that hassle and significantly reduce the effort needed to build AI data pipelines.

Key features:

  • Get responses that comply with your specified JSON schema
  • No need for post-processing, can be directly integrated into data pipelines
  • Each token is automatically validated against the schema

Basic Usage

Cortex COMPLETE Structured Outputs is used by specifying the response_format in the options argument of the COMPLETE function:

SELECT SNOWFLAKE.CORTEX.COMPLETE('model_name', [
    {
        'role': 'user',
        'content': 'prompt'
    }
],
{
    'temperature': 0,
    'max_tokens': 1000,
    'response_format': {
        'type': 'json',
        'schema': {
            'type': 'object',
            'properties': {
                'property_name': {
                    'type': 'string'
                },
                ...
            },
            'required': ['required_property_name', ...]
        }
    }
});
Enter fullscreen mode Exit fullscreen mode

Practical Examples with Game Data Generation

Example 1: Roguelike RPG Room Generation

Here's an example for game developers generating dungeon room information:

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-3-5-sonnet', [
    {
        'role': 'user',
        'content': 'Generate one room for a dark fantasy themed roguelike RPG.'
    }
],
{
    'temperature': 0.7,
    'max_tokens': 800,
    'response_format': {
        'type': 'json',
        'schema': {
            'type': 'object',
            'properties': {
                'room_name': {
                    'type': 'string',
                    'description': 'Name of the room'
                },
                'description': {
                    'type': 'string',
                    'description': 'Detailed description of the room'
                },
                'monsters': {
                    'type': 'array',
                    'items': {
                        'type': 'object',
                        'properties': {
                            'name': {
                                'type': 'string'
                            },
                            'hp': {
                                'type': 'number'
                            },
                            'attack': {
                                'type': 'number'
                            }
                        }
                    }
                },
                'treasure': {
                    'type': 'object',
                    'properties': {
                        'gold': {
                            'type': 'number'
                        },
                        'items': {
                            'type': 'array',
                            'items': {
                                'type': 'string'
                            }
                        }
                    }
                }
            },
            'required': ['room_name', 'description', 'monsters']
        }
    }
});
Enter fullscreen mode Exit fullscreen mode

Running this query produces results like:

{
  "room_name": "Altar of Corruption",
  "description": "What was once a sacred altar is now shrouded in corruption and darkness. Green slime drips from the walls, and ancient human bones are scattered across the floor. In the center of the room stands an ominous altar made of black stone, with something appearing to pulse on top of it.",
  "monsters": [
    {
      "name": "Corrupted Priest",
      "hp": 120,
      "attack": 25
    },
    {
      "name": "Bone Familiar",
      "hp": 40,
      "attack": 15
    }
  ],
  "treasure": {
    "gold": 250,
    "items": [
      "Cursed Amulet",
      "Healing Potion",
      "Staff of Corruption"
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

What do you think? You can see that the information about a dungeon room in an RPG, including monster names, HP, attack power, and treasure information, is output according to the specified JSON schema.

Providing detailed instructions in the description field can yield more accurate outputs. The required field specifies mandatory fields, and if the LLM cannot generate these fields, the function will return an error.

Example 2: Character Ability Generation

Here's an example for automatically generating RPG characters with more detailed instructions in the description:

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-3-5-sonnet', [
    {
        'role': 'user',
        'content': 'Generate a random player character for a fantasy RPG.'
    }
],
{
    'temperature': 0.8,
    'max_tokens': 1000,
    'response_format': {
        'type': 'json',
        'schema': {
            'type': 'object',
            'properties': {
                'name': {
                    'type': 'string',
                    'description': 'English name within 6 characters'
                },
                'race': {
                    'type': 'string',
                    'description': 'Elf, Dwarf, Human, Orc, etc.'
                },
                'class': {
                    'type': 'string',
                    'description': 'Warrior, Wizard, Rogue, Cleric, etc.'
                },
                'stats': {
                    'type': 'object',
                    'properties': {
                        'hp': {
                            'type': 'number',
                            'description': 'Should be in the range of 10 - 30'
                        },
                        'strength': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 15'
                        },
                        'dexterity': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 15'
                        },
                        'constitution': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 15'
                        },
                        'intelligence': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 15'
                        },
                        'wisdom': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 15'
                        },
                        'charisma': {
                            'type': 'number',
                            'description': 'Should be in the range of 1 - 3'
                        }
                    }
                },
                'background': {
                    'type': 'string',
                    'description': 'Describe the reason for adventuring, considering race and class'
                }
            },
            'required': ['name', 'race', 'class', 'stats']
        }
    }
});
Enter fullscreen mode Exit fullscreen mode

This query produces results like:

{
  "created": 1740492411,
  "model": "claude-3-5-sonnet",
  "structured_output": [
    {
      "raw_message": {
        "background": "Immersed in magical research in the Elven forest, but decided to embark on an adventure seeking deeper knowledge. Aims to unravel magical secrets using high intelligence and agility.",
        "class": "Wizard",
        "name": "Thalia",
        "race": "Elf",
        "stats": {
          "charisma": 2,
          "constitution": 8,
          "dexterity": 13,
          "hp": 15,
          "intelligence": 15,
          "strength": 6,
          "wisdom": 12
        }
      },
      "type": "json"
    }
  ],
  "usage": {
    "completion_tokens": 99,
    "prompt_tokens": 1122,
    "total_tokens": 1221
  }
}
Enter fullscreen mode Exit fullscreen mode

Nice! While adherence to the description depends on the LLM's discretion, we've received output that meets our expectations.

Conclusion

What do you think of Cortex COMPLETE Structured Outputs? Although my examples might be a bit hobby-oriented, I hope they've given you a good idea of how to use this feature. This functionality truly shines in actual analysis and business scenarios. For example, it can be used to summarize unstructured business logs into structured reports, automatically generate FAQ tables, perform data cleansing for subsequent pipelines, and much more. Please give it a try!

Promotion

Snowflake What's New Updates on X

I'm sharing updates on Snowflake's What's New on X. I'd be happy if you could follow:

English Version

Snowflake What's New Bot (English Version)

Japanese Version

Snowflake's What's New Bot (Japanese Version)

Change Log

(20250225) Initial post

Original Japanese Article

https://zenn.dev/tsubasa_tech/articles/6ecf997a77c7c0

Top comments (0)