Fix: Collection Contains Entries of a Different Type in Astro

Error message:
Collection contains entries of a different type.
Content Collections 2025-01-25

What Causes This Error?

This error occurs when you configure a collection with one type but the files in the collection folder are of a different type. For example, setting type: 'content' but having JSON files, or type: 'data' with Markdown files.

The Problem

// src/content/config.ts
const authors = defineCollection({
  type: 'data',  // Expects JSON/YAML files
  schema: z.object({ /* ... */ }),
});
src/content/authors/
├── john.md      # ❌ Markdown file in data collection
└── jane.json    # ✅ Correct

The Fix

Match File Types to Collection Type

// For JSON/YAML files
const authors = defineCollection({
  type: 'data',
  schema: z.object({
    name: z.string(),
    email: z.string(),
  }),
});
src/content/authors/
├── john.json    # ✅ Data file
└── jane.yaml    # ✅ Data file
// For Markdown/MDX files
const blog = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string(),
  }),
});
src/content/blog/
├── post-1.md    # ✅ Content file
└── post-2.mdx   # ✅ Content file

Common Scenarios

Data Collection with Wrong Files

# ❌ Problem: Markdown in data collection
src/content/settings/
└── site.md

# ✅ Solution: Use JSON or YAML
src/content/settings/
└── site.json

Content Collection with Wrong Files

# ❌ Problem: JSON in content collection
src/content/blog/
└── config.json

# ✅ Solution: Move to data collection
src/content/blog-config/
└── config.json

Converting Markdown to JSON

---
# Before: src/content/authors/john.md
name: John Doe
email: john@example.com
---

This is John's bio.
// After: src/content/authors/john.json
// (Note: body content is lost in data collections)
{
  "name": "John Doe",
  "email": "john@example.com",
  "bio": "This is John's bio."
}

Converting JSON to Markdown

// Before: src/content/blog/post.json
{
  "title": "My Post",
  "content": "Post content here"
}
---
# After: src/content/blog/post.md
title: My Post
---

Post content here

Checking Collection Type

// src/content/config.ts
import { defineCollection, z } from 'astro:content';

// Type: 'content' for files with body
// Supports: .md, .mdx
const blog = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string(),
  }),
});

// Type: 'data' for pure data files
// Supports: .json, .yaml, .toml
const settings = defineCollection({
  type: 'data',
  schema: z.object({
    key: z.string(),
  }),
});

export const collections = { blog, settings };

File Extension Reference

Collection TypeSupported Extensions
content.md, .mdx
data.json, .yaml, .yml, .toml

Handling Mixed Needs

// If you need both content and metadata for the same concept,
// use two collections

const posts = defineCollection({
  type: 'content',  // Markdown posts
  schema: z.object({
    title: z.string(),
    authorId: z.string(),
  }),
});

const authors = defineCollection({
  type: 'data',  // Author data
  schema: z.object({
    name: z.string(),
    avatar: z.string(),
  }),
});

Ignoring Files

// Files starting with _ are ignored
src/content/blog/
├── post.md       # ✅ Included
├── _draft.md     # Ignored
└── _config.json  # Ignored

Quick Checklist

  • type: 'content' → use .md or .mdx files
  • type: 'data' → use .json, .yaml, or .toml files
  • Don’t mix file types in one collection
  • Use _ prefix to ignore files
  • Create separate collections for different types