Skip to content

refactor: make BaseProvider a BaseModel with discriminated union#5323

Merged
greysonlalonde merged 1 commit into
mainfrom
refactor/provider-basemodel
Apr 7, 2026
Merged

refactor: make BaseProvider a BaseModel with discriminated union#5323
greysonlalonde merged 1 commit into
mainfrom
refactor/provider-basemodel

Conversation

@greysonlalonde

@greysonlalonde greysonlalonde commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace BaseProvider Protocol with BaseModel + ABC so providers serialize/deserialize natively via pydantic
  • Add provider_type Literal field to JsonProvider and SqliteProvider
  • Use discriminated union on CheckpointConfig.provider so the correct provider class is reconstructed from checkpoint JSON
  • Fixes TypeError: Expected a BaseProvider instance, got <class 'str'> on checkpoint restore

Test plan

  • CheckpointConfig(provider=SqliteProvider()) round-trips through model_dump_json / model_validate_json
  • Crew.from_checkpoint(path, provider=SqliteProvider()) works
  • Existing checkpoint tests pass

Note

Medium Risk
Medium risk because it changes how checkpoint providers are typed/validated/serialized via Pydantic, which can affect checkpoint round-tripping and restore paths across existing stored snapshots.

Overview
Refactors checkpoint provider serialization by replacing BaseProvider from a Protocol with an abstract pydantic.BaseModel carrying a provider_type discriminator.

CheckpointConfig.provider is now a discriminated union (JsonProvider | SqliteProvider) keyed by provider_type, and both providers add a provider_type Literal field so checkpoint configs can round-trip through JSON and reconstruct the correct provider implementation during restore.

Reviewed by Cursor Bugbot for commit 45e05e2. Bugbot is set up for automated code reviews on this repo. Configure here.

Replace the Protocol with a BaseModel + ABC so providers serialize and
deserialize natively via pydantic. Each provider gets a Literal
provider_type field. CheckpointConfig.provider uses a discriminated
union so the correct provider class is reconstructed from checkpoint JSON.
@github-actions github-actions Bot added the size/S label Apr 7, 2026

@iris-clawd iris-clawd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: BaseProvider → BaseModel with discriminated union

Good fix for the serialization round-trip issue. Protocol → BaseModel+ABC is the right call when you need Pydantic to own the serialization.

🟡 Extensibility for custom providers

The discriminated union on CheckpointConfig.provider is now a concrete JsonProvider | SqliteProvider. Third-party providers can't be plugged in without modifying this type annotation. The old Protocol approach allowed any conforming object.

For alpha this is fine — you only have two providers. But if custom providers are a planned use case, you'd want a registry pattern or Union that can be extended (e.g., entry points, or a class method that returns the updated Annotated type). Worth noting in docs.

🟡 BaseProvider.provider_type default

BaseProvider has provider_type: str = "base" as a non-abstract field. A subclass that forgets to override it with a Literal would still pass validation but break the discriminator at the CheckpointConfig level (Pydantic would fail to match "base" to any union member). Making provider_type abstract or removing the default would catch this at definition time. Minor since you only have two concrete providers.

🟢 Looks good

  • Discriminated union via provider_type Literal is clean Pydantic v2 pattern
  • @abstractmethod on all five methods is correct — forces implementation
  • Removes the hacky __get_pydantic_core_schema__ workaround
  • Fixes the actual bug (TypeError on checkpoint restore)

Solid refactor. 💬 171

@iris-clawd iris-clawd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. ✅

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 45e05e2. Configure here.

Comment thread lib/crewai/src/crewai/state/checkpoint_config.py
@greysonlalonde greysonlalonde merged commit 75f162f into main Apr 7, 2026
51 of 83 checks passed
@greysonlalonde greysonlalonde deleted the refactor/provider-basemodel branch April 7, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants