Skip to content

Feature: Memory-backed extraction — recall past structured outputs for few-shot context #2403

Description

@ferhimedamine

Instructor extracts structured data from LLMs precisely, but every extraction is independent — no shared memory means repeated similar extractions re-do the same work. This proposes Dakera (https://dakera.ai) as an optional memory layer for memory-backed few-shot extraction.

Problem: When using Instructor to extract the same class of structured data repeatedly, high-quality past extractions are never used as few-shot examples. Each extraction starts from scratch even if similar data was extracted successfully yesterday.

Proposed: memory= parameter on client.chat.completions.create:

import instructor
from openai import OpenAI
from pydantic import BaseModel
from dakera import DakeraClient

class InvoiceData(BaseModel):
vendor: str
total: float
date: str

memory = DakeraClient(base_url='http://localhost:3300', api_key='demo')
client = instructor.from_openai(OpenAI())

def extract_with_memory(text: str, agent_id: str = 'invoice-extractor') -> InvoiceData:
# Recall prior successful extractions as few-shot context
prior = memory.recall(agent_id=agent_id, query=text[:200], top_k=2)
system = 'Extract structured invoice data.'
if prior and prior.memories:
examples = '\n'.join(f'Example: {m.content}' for m in prior.memories)
system = f'{system}\n\nPrior successful extractions:\n{examples}'

result = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'system', 'content': system}, {'role': 'user', 'content': text}],
    response_model=InvoiceData,
)

# Store successful extraction for future recall
memory.store_memory(agent_id=agent_id, content=result.model_dump_json(), metadata={'source': text[:100]})
return result

Setup: docker run -d -p 3300:3300 -e DAKERA_API_KEY=demo ghcr.io/dakera-ai/dakera:latest / pip install dakera

This could be added as a cookbook recipe or optional InstructorWithMemory wrapper.

Happy to open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions