Skip to main content

Array Generation

Generate lists of structured objects with consistent schemas. Arrays enable creation of sequential content, batch processing, and hierarchical data structures.

Overview

Array generation allows you to:

  • Generate multiple items with the same structure
  • Create sequential content like chapters, sections, or episodes
  • Build hierarchical data with nested arrays
  • Control item count with specific instructions
  • Maintain consistency across all generated items

Book Generation Example

This real-world example generates a complete book structure with chapters and pages, demonstrating nested arrays and processing order control.

curl -X POST http://localhost:2008/objectGen \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"prompt": "Create a science fiction novel about humanity'\''s first contact with an alien civilization",
"definition": {
"type": "object",
"instruction": "Creating a fictional story given the story outline provided. This story needs to be engaging and interesting to read.",
"properties": {
"title": {
"type": "string",
"instruction": "Return the title of the book which encapsulates the story. Return the title in a maximum of 5 words."
},
"chapters": {
"type": "array",
"instruction": "Create a list of 3 chapters with detailed outlines. Each chapter will have 2 pages. Write a minimum of 100 words for each chapter outlining what will happen.",
"items": {
"type": "object",
"instruction": "Create the details of the chapter so that the story being written will feel real and lived in.",
"processingOrder": ["title", "outline", "pages"],
"properties": {
"title": {
"type": "string",
"instruction": "Create a title for the chapter which must be less than 5 words."
},
"outline": {
"type": "string",
"instruction": "Create an outline of what will occur in this chapter. Return a minimum of 100 words for this outline. This outline should cover what will occur in 2 pages."
},
"pages": {
"type": "array",
"instruction": "Create a list of 2 pages, where each page outline should be detailed enough to ensure that each one logically leads to the next, creating a satisfying narrative progression. Return a maximum of 2 items.",
"items": {
"type": "object",
"instruction": "Create the details and information for a single page within a book.",
"processingOrder": ["outline", "finalContent"],
"properties": {
"outline": {
"type": "string",
"instruction": "Create detailed overview of what content this page should include. This page outline should be detailed enough to ensure that it will logically lead to the next page. This outline should be a minimum of 250 words.",
"systemPrompt": "You are an author AI assistant. You are not a chatbot. Follow the instructions given and ensure the asked details are created."
},
"finalContent": {
"type": "string",
"instruction": "Create a vivid and immersive page for the fiction book. Focus on detailed descriptions, engaging character dialogue, and dynamic reactions to events. Return a page which has the length of around 500 words. Only return the narrative content for the page without any additional explanations.",
"narrowFocus": {
"fields": ["outline"],
"prompt": "Use the page outline to create the final narrative content. Maintain consistency with the chapter outline and writing style."
}
}
}
}
}
}
}
}
}
}
}'

Array Definition Structure

Basic Array Configuration

{
"type": "array",
"instruction": "Create a list of N items with specific requirements",
"items": {
"type": "object",
"properties": {
// Item schema definition
}
}
}

Key Properties

FieldTypeDescription
typestringMust be "array"
instructionstringDescribe what items to generate and how many
itemsobjectSchema definition for each array item

Expected Response Structure

Arrays are returned as list_value with nested structures:

{
"data": {
"fields": {
"title": {
"string_value": "First Contact Protocol"
},
"chapters": {
"list_value": {
"values": [
{
"struct_value": {
"fields": {
"title": {
"string_value": "The Signal"
},
"outline": {
"string_value": "Dr. Sarah Chen detects an anomalous radio signal..."
},
"pages": {
"list_value": {
"values": [
{
"struct_value": {
"fields": {
"outline": {
"string_value": "Introduction to Dr. Chen at the observatory..."
},
"finalContent": {
"string_value": "The observatory hummed with quiet efficiency..."
}
}
}
}
]
}
}
}
}
}
]
}
}
}
},
"usdCost": 0.15
}

Response Navigation

  • Arrays: list_value.values contains an array of items
  • Objects in arrays: Each item is a struct_value with fields
  • Nested arrays: Can contain additional list_value structures
  • String values: Accessed via string_value property

Advanced Features

Processing Order

Control the sequence in which fields are generated within array items:

{
"type": "array",
"items": {
"type": "object",
"processingOrder": ["title", "outline", "content"],
"properties": {
"title": { "type": "string" },
"outline": { "type": "string" },
"content": { "type": "string" }
}
}
}

This ensures each item generates its title first, then outline, then content - allowing later fields to reference earlier ones.

Narrow Focus

Use narrowFocus to have fields reference previously generated content:

{
"finalContent": {
"type": "string",
"instruction": "Write the full content",
"narrowFocus": {
"fields": ["outline"],
"prompt": "Use the outline to create the final content"
}
}
}

This explicitly passes the outline field to the generation context for finalContent.

Nested Arrays

Arrays can contain other arrays for hierarchical structures:

{
"chapters": {
"type": "array",
"items": {
"type": "object",
"properties": {
"pages": {
"type": "array",
"items": {
"type": "object",
"properties": {
"paragraphs": {
"type": "array",
"items": { "type": "string" }
}
}
}
}
}
}
}
}

Use Cases

  1. Content Series: Generate blog posts, chapters, or episodes
  2. Product Catalogs: Create consistent product descriptions
  3. Educational Content: Build lesson plans with multiple sections
  4. Test Data: Generate structured test datasets
  5. Documentation: Create API documentation for multiple endpoints
  6. Creative Writing: Build novels, scripts, or story outlines

Best Practices

  1. Specify Count: Clearly state how many items to generate in the instruction
  2. Use Processing Order: Control field generation sequence for better context
  3. Leverage Narrow Focus: Pass relevant context between fields
  4. Set Constraints: Define word counts, character limits, or formatting rules
  5. Nested Arrays: Keep hierarchy depth reasonable (2-3 levels max)
  6. System Prompts: Use different system prompts for different roles (author, editor, etc.)
  7. Consistency: Ensure instructions maintain consistency across all items
tip

For large arrays (50+ items), consider generating a detailed outline first, then using it to guide the full content generation with narrowFocus.

warning

Very large nested arrays can result in high token usage. Start with smaller counts to test your schema before scaling up.

Real-World Performance

The book generation example demonstrates:

  • 3 chapters with 2 pages each = 6 total pages
  • Each page generates ~750 words (250 word outline + 500 word content)
  • Total output: ~4,500 words of structured, coherent narrative
  • Processing time: Varies based on model and complexity

Next Steps