Skip to main content

Open Agent Memory Protocol (OAMP) types for Python

Project description

oamp-types

Python types for the Open Agent Memory Protocol (OAMP) v1.0.0.

Built on Pydantic v2 for validation and serialization.

Install

pip install oamp-types

Quick Start

from oamp_types import (
    KnowledgeEntry,
    KnowledgeCategory,
    KnowledgeSource,
    KnowledgeStore,
    UserModel,
    CommunicationProfile,
    ExpertiseDomain,
    ExpertiseLevel,
    Correction,
    StatedPreference,
    validate_knowledge_entry,
    validate_knowledge_store,
    validate_user_model,
)

# Create a knowledge entry
entry = KnowledgeEntry(
    user_id="user-123",
    category=KnowledgeCategory.correction,
    content="Never use unwrap() — use ? operator instead",
    confidence=0.98,
    source=KnowledgeSource(session_id="session-42"),
)

# Validate
errors = validate_knowledge_entry(entry)
assert len(errors) == 0

# Serialize to JSON (exclude null optional fields for spec compliance)
json_str = entry.model_dump_json(exclude_none=True)

# Parse from dict or JSON
parsed = KnowledgeEntry.model_validate_json(json_str)

# Create a user model
model = UserModel(user_id="user-123")
model.communication = CommunicationProfile(
    verbosity=-0.6,
    formality=0.3,
    prefers_examples=True,
    prefers_explanations=False,
    languages=["en", "de"],
)
model.expertise.append(
    ExpertiseDomain(
        domain="rust",
        level=ExpertiseLevel.expert,
        confidence=0.95,
    )
)

# Bulk export
store = KnowledgeStore(user_id="user-123", entries=[entry])

Serialization

Pydantic models support multiple serialization modes:

# Exclude None values (matches Rust's skip_serializing_if = "Option::is_none")
json_str = entry.model_dump_json(exclude_none=True)

# Include all fields (useful for debugging)
json_str = entry.model_dump_json()

# Python dict output
d = entry.model_dump(exclude_none=True)

Note: For spec-compliant output, use exclude_none=True to omit optional fields that are not set (e.g., decay, agent_id). This matches the behavior of the Rust and TypeScript reference implementations.

Validation

Two levels of validation are provided:

Pydantic field validators (automatic)

Creating or parsing models through Pydantic automatically validates:

  • Required fields must be present
  • oamp_version must be "1.0.0"
  • type must match the expected document type
  • confidence must be in [0.0, 1.0]
  • String fields must not be empty where required
  • Unknown fields are rejected (extra = "forbid")
  • Ranges like verbosity (-1.0 to 1.0) and formality (-1.0 to 1.0)

Semantic validation functions (for data bypassing Pydantic)

from oamp_types import validate_knowledge_entry, validate_knowledge_store, validate_user_model

# Returns list of error strings; empty list means valid
errors = validate_knowledge_entry(entry)
if errors:
    raise ValueError(f"Validation failed: {errors}")

These functions validate data that may have been constructed via model_construct() or loaded from untrusted sources where Pydantic's field validators were bypassed.

API

KnowledgeEntry

Field Type Required Description
oamp_version str Defaults to "1.0.0"
type str Defaults to "knowledge_entry"
id str UUID v4, auto-generated
user_id str User identifier
category KnowledgeCategory fact, preference, pattern, correction
content str Natural language knowledge
confidence float 0.0–1.0
source KnowledgeSource Provenance info
decay KnowledgeDecay | None Temporal decay params
tags list[str] Free-form tags
metadata dict[str, Any] Vendor extensions

UserModel

Field Type Required Description
oamp_version str Defaults to "1.0.0"
type str Defaults to "user_model"
user_id str User identifier
model_version int ≥ 1, defaults to 1
updated_at datetime Auto-set to now (UTC)
communication CommunicationProfile | None Communication preferences
expertise list[ExpertiseDomain] Domain expertise
corrections list[Correction] Agent corrections
stated_preferences list[StatedPreference] Declared preferences
metadata dict[str, Any] Vendor extensions

Python Version

Requires Python ≥ 3.9. Uses str, Enum pattern for category/level enums (available since Python 3.11 as StrEnum, backported for 3.9 compatibility).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oamp_types-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oamp_types-1.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file oamp_types-1.0.0.tar.gz.

File metadata

  • Download URL: oamp_types-1.0.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for oamp_types-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a33a9a9e65bc4b963805e195c1d66d43bdd97808172a978f031b9378f4edcbcb
MD5 d738797def7a1901a033d4d2deed92d0
BLAKE2b-256 d84fe4cb0f003e59bc8dc9cf8df40412a9551e2f44fcffe85be5ba02c5e17e56

See more details on using hashes here.

File details

Details for the file oamp_types-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: oamp_types-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for oamp_types-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78b4f0599312d69cf4c8f51a28ee769eda37b87721e1613d5aa881ad8cc1f751
MD5 0447f9285d4655924ec6e9a963ce83bf
BLAKE2b-256 c1741b6e42cee99e5af6f5713c45243bef6fc4678ac578e1249dc05ca9c829f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page