tonyandrewmeyer/charming-with-claude/relation-data-classes
Claude Code and agent skill by tonyandrewmeyer/charming-with-claude
- community
Checking that the SKILL.md file is still in the repository.
Install
npx vskill@latest install tonyandrewmeyer/charming-with-claude/relation-data-classesSKILL.md
The same SKILL.md was published earlier as tonyandrewmeyer/charming-with-claude/relation-data-classes.
Skill: Adopt Relation Data Classes
Replace raw relation.data[app/unit]["key"] dictionary access with typed Python classes using Relation.save() and Relation.load().
When to Use
Use this when a charm reads or writes relation data bags directly using string keys and manual JSON serialisation. Do not convert relation data that is managed by a charm library.
Prerequisites
- ops >= 2.23.0
- The charm has relations defined in
charmcraft.yaml(ormetadata.yaml)
Step 1: Identify Convertible Relation Data
- List all relations in the charm's metadata.
- For each relation, search charm code for direct data access:
relation.data[self.app]["key"](writing app data)relation.data[self.unit]["key"](writing unit data)relation.data[event.app]/relation.data[event.unit](reading)json.dumps/json.loadsused for serialisation
- Exclude any relation data managed by charm libraries (e.g.
charms.data_platform_libs,charms.observability_libs). Those libraries own the data format.
Step 2: Define Data Classes
For each relation endpoint where the charm owns the data format, define a typed class.
For Pydantic (recommended for complex data or when you need validation):
Alias Support
Relation data keys may use dashes or other conventions. Use aliases to map them:
Step 3: Update Charm Code
Writing relation data
Important: always check self.unit.is_leader() before saving to app data bags.
Reading relation data
Custom serialisation
The default encoder/decoder is json.dumps/json.loads. For custom formats:
Step 4: Update Tests
Tests that set relation data in testing.Relation should use the same serialised format that save() produces:
Step 5: Verify
- Run unit tests to verify relation data is correctly read and written.
- Crucially: verify that the serialised format hasn't changed. The remote charm must be able to read the data. Run
juju show-unitto compare the data bag format before and after.
Exemplar Charms
- [charmlibs](https://github.com/canonical/charmlibs) (interfaces/sloth) — Pydantic with custom YAML encoder/decoder for complex nested data.
- [hive-metastore-k8s-operator](https://github.com/canonical/hive-metastore-k8s-operator) — Pydantic with aliases and a custom decoder that resolves Juju secrets.
- [falco-operators](https://github.com/canonical/falco-operators) (interfaces/falcosidekick_http_endpoint) — Clean minimal example with
HttpUrlvalidation.
Common Mistakes
- Don't change the wire format. The serialised keys and value formats must remain compatible with what remote charms expect. Test this carefully.
- Don't convert library-managed relation data. If a charm library (e.g. from
charms.data_platform_libs) handles the relation, leave it alone. - Don't forget leader gating. Only the leader unit can write to app data bags.
- Do handle `pydantic.ValidationError` (or
ValueErrorfor dataclasses) gracefully when loading data from remote charms — the remote may send unexpected data.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingexperiments/2026-01-25-ops-upgrade/skills/relation-data-classes/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/tonyandrewmeyer/charming-with-claude/relation-data-classes)