# Discover Your Schema

Source: https://docs.rushdb.com/2.x/learn/records-and-queries/discover-your-schema/

RushDB is **self-aware** — it continuously understands its own structure: labels, field types, value distributions, relationships, and semantic search readiness. The `db.ai` schema methods expose this as either Markdown (for LLM injection) or structured JSON (for schema UIs, autocomplete, or property-ID lookups).

---

## What the Schema Contains

The schema is RushDB's always-current answer to "what does this project's data look like?" — computed from the graph itself, never declared upfront.

| Component                       | Description                                                                                             |
| ------------------------------- | ------------------------------------------------------------------------------------------------------- |
| **Label inventory**             | All label names in the project, with record counts                                                      |
| **Property manifest per label** | Property name, type, and either sample values (strings/booleans) or a min–max range (numbers/datetimes) |
| **Relationship map**            | Which labels connect to which, via which relationship type, and in which direction                      |
| **Semantic index status**       | Which properties have embedding indexes and whether they are ready for `db.records.vectorSearch()`      |

Building an AI agent? [Schema Self-Awareness](/learn/agent-memory/schema-self-awareness) shows what the rendered output looks like and how to inject it into LLM context.

---

## Get Schema as Markdown

The **recommended format for LLM context injection** — compact, token-efficient, and ready to paste into a system prompt.

**Python**

`db.ai.get_schema_markdown()`

```python
from rushdb import RushDB

db = RushDB("RUSHDB_API_KEY")

# Full schema as a Markdown document
response = db.ai.get_schema_markdown()
schema = response.data

# Scope to specific labels
order_response = db.ai.get_schema_markdown({"labels": ["Order"]})

# Bypass the 1-hour cache and force a fresh recalculation
fresh_response = db.ai.get_schema_markdown({"force": True})
```

**TypeScript**

`db.ai.getSchemaMarkdown()`

```typescript
// Full schema as a Markdown document
const { data: schema } = await db.ai.getSchemaMarkdown()

// Scope to specific labels
const { data: orderSchema } = await db.ai.getSchemaMarkdown({
  labels: ['Order']
})

// Bypass the 1-hour cache and force a fresh recalculation
const { data: freshSchema } = await db.ai.getSchemaMarkdown({ force: true })
```

**Shell**

`POST /api/v1/ai/schema/md`

```bash
# Full schema
curl -X POST https://api.rushdb.com/api/v1/ai/schema/md \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -d '{}'

# Scoped to specific labels
curl -X POST https://api.rushdb.com/api/v1/ai/schema/md \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -d '{"labels": ["Order"]}'

# Force cache bypass
curl -X POST https://api.rushdb.com/api/v1/ai/schema/md \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -d '{"force": true}'
```

#### Request body fields

| Field    | Type       | Required | Description                                                                  |
| -------- | ---------- | -------- | ---------------------------------------------------------------------------- |
| `labels` | `string[]` | no       | Restrict output to specific labels. Omit (or pass `[]`) for the full schema. |
| `force`  | `boolean`  | no       | Bypass the 1-hour cache and force a full recalculation.                      |

See a complete rendered example — graph, Markdown, and JSON side by side — in [Schema Self-Awareness](/learn/agent-memory/schema-self-awareness#what-the-agent-receives).

---

## Get Schema (Structured JSON)

Returns the same schema as a structured array — useful for schema UIs, autocomplete, or looking up property IDs for `db.properties.values()`.

**Python**

`db.ai.get_schema()`

```python
# List all labels with counts
response = db.ai.get_schema()
for item in response.data:
    print(f"{item['label']}: {item['count']} records")

# Scope to specific labels
book_response = db.ai.get_schema({"labels": ["Book"]})
book_schema = book_response.data[0]

# Get property ID for value enumeration
genre_prop = next(p for p in book_schema["properties"] if p["name"] == "genre")
genres = db.properties.values(genre_prop["id"])

# Identify semantically searchable properties
indexed = [p for p in book_schema["properties"] if p.get("vectorIndexes")]
# indexed[0]["vectorIndexes"][0]["status"] == "ready" → queryable with db.records.vector_search()

# Bypass the 1-hour cache
fresh_response = db.ai.get_schema({"force": True})
```

**TypeScript**

`db.ai.getSchema()`

```typescript
// List all labels with counts
const { data: schema } = await db.ai.getSchema()
for (const item of schema) {
  console.log(`${item.label}: ${item.count} records`)
}

// Get property ID for value enumeration
const {
  data: [bookSchema]
} = await db.ai.getSchema({ labels: ['Book'] })
const genreProp = bookSchema.properties.find((p) => p.name === 'genre')
const { data: genres } = await db.properties.values({ id: genreProp.id })

// Identify semantically-searchable properties
const indexed = bookSchema.properties.filter((p) => p.vectorIndexes?.length)
// indexed[0].vectorIndexes[0].status === 'ready' → queryable with db.records.vectorSearch()

// Bypass the 1-hour cache
const { data: fresh } = await db.ai.getSchema({ force: true })
```

**Shell**

`POST /api/v1/ai/schema`

```bash
# Full schema (structured JSON)
curl -X POST https://api.rushdb.com/api/v1/ai/schema \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -d '{}'

# Scoped to specific labels
curl -X POST https://api.rushdb.com/api/v1/ai/schema \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -d '{"labels": ["Book"]}'
```

### TypeScript types

```typescript
type SchemaItem = {
  label: string
  count: number
  properties: SchemaProperty[]
  relationships: SchemaRelationship[]
}

type SchemaProperty = {
  id: string // use with db.properties.values()
  name: string
  type: string // 'string' | 'number' | 'boolean' | 'datetime'
  isArray?: boolean // true when records store this property as an array of primitive values
  values?: Array<string | number> // up to 10 samples (string/boolean only)
  min?: number | string // number/datetime only
  max?: number | string
  /** Non-empty when embedding indexes exist — property is queryable with db.records.vectorSearch() */
  vectorIndexes?: SchemaVectorIndex[]
}

type SchemaVectorIndex = {
  id: string
  sourceType: string // 'managed' | 'external'
  similarityFunction: string // 'cosine' | 'euclidean'
  dimensions: number
  status: string // 'pending' | 'indexing' | 'ready' | 'error'
  modelKey: string
}

type SchemaRelationship = {
  label: string
  type: string
  direction: 'in' | 'out'
  count?: number
  properties?: Array<{
    name: string
    type: string
    values?: Array<string | number | boolean>
    min?: number | string
    max?: number | string
    relationshipsCount: number
  }>
}
```

---

## Caching

:::note Caching
Both methods share a **1-hour cache** per project. The first call after TTL expiry triggers a full graph scan; all subsequent calls within the hour are instant. Pass `{ force: true }` to bypass the cache and trigger an immediate recalculation.
:::

---

## See also

- [Schema Self-Awareness](/learn/agent-memory/schema-self-awareness) — inject the schema into LLM context, dynamic query construction, and Agent Skills
- [Labels & Properties](/learn/records-and-queries/labels-and-properties) — enumerate schema at a low level
- [Semantic Search](/learn/semantic-search) — use indexed properties for similarity search
- [Manage Embedding Indexes](/learn/semantic-search/manage-indexes) — view index status in schema output
