Store Instructor-extracted structured outputs in CortexDB as typed experiences.

Instructor Integration

Instructor extracts typed Pydantic objects from LLM responses. CortexDB stores them as durable, queryable memory — either as JSON content or directly as fact triples.

Install

pip install cortexdbai instructor pydantic

Pattern — capture extracted models as triples

import os, instructor
from datetime import datetime, timezone
from uuid import uuid4
from openai import OpenAI
from pydantic import BaseModel
from cortexdb.v1 import V1Client

llm     = instructor.from_openai(OpenAI())
cortex  = V1Client(api_url="https://api-v1.cortexdb.ai", actor="user:alice",
                   bearer=os.environ["CORTEX_TOKEN"])
SCOPE   = "org:acme/user:alice"


class DealUpdate(BaseModel):
    customer:   str
    deal_stage: str
    seats:      int

extracted = llm.chat.completions.create(
    model="gpt-4o",
    response_model=DealUpdate,
    messages=[{"role": "user", "content": "Acme just bumped to 200 seats and signed."}],
)

# Capture as JSON for inspection
cortex.experience(
    scope=SCOPE,
    modality="tool_result",
    content_kind="json",
    content_data=extracted.model_dump(),
    observed_at=datetime.now(timezone.utc).isoformat(),
    idempotency_key=f"inst-{uuid4()}",
)

# Or capture as a fact triple
cortex.experience(
    scope=SCOPE,
    modality="observation",
    content_kind="triple",
    triple={
        "subject":   {"type": "entity", "id": f"ent_{extracted.customer}"},
        "predicate": "deal_stage",
        "object":    {"datatype": "string", "value": extracted.deal_stage},
    },
    observed_at=datetime.now(timezone.utc).isoformat(),
    idempotency_key=f"inst-fact-{uuid4()}",
)

See also