Skip to content

Python: allow registering custom types for checkpoint serialization and deserialization - #7449

Open
Mahajan-Sachin wants to merge 3 commits into
microsoft:mainfrom
Mahajan-Sachin:feature-checkpoint-custom-types
Open

Python: allow registering custom types for checkpoint serialization and deserialization#7449
Mahajan-Sachin wants to merge 3 commits into
microsoft:mainfrom
Mahajan-Sachin:feature-checkpoint-custom-types

Conversation

@Mahajan-Sachin

Copy link
Copy Markdown

Motivation & Context

In hosting environments like Foundry, developers don't manage the checkpoint store directly. If custom types are used in their workflows, checkpoint serialization/deserialization will fail because the restricted unpickler rejects any unknown type for security reasons. Currently, there is no way to extend allowed_checkpoint_types without direct access to the checkpoint storage configuration.

This PR introduces a process-wide global registry for checkpoint serialization, similar to the existing register_state_type utility used in agent sessions, allowing developers to register custom classes or string type keys.

Description & Review Guide

What are the major changes?

  • Created a global _CUSTOM_ALLOWED_TYPES registry set in packages/core/agent_framework/_workflows/_checkpoint_encoding.py.
  • Added the public register_checkpoint_type(cls_or_key: type[Any] | str) function to allow developers to register classes or string keys.
  • Updated _RestrictedUnpickler.find_class and _is_allowed_type to allow types registered in _CUSTOM_ALLOWED_TYPES.
  • Exposed register_checkpoint_type under the core package root __init__.py.
  • Added unit tests in packages/core/tests/workflow/test_checkpoint_unrestricted_pickle.py verifying registration behavior, string key registration, and input validation.

What is the impact of these changes?
Developers can now call register_checkpoint_type(MyCustomClass) at module import time. This allows their custom types to pass through checkpoint serialization in hosting environments without needing direct access to storage initializers.

What do you want reviewers to focus on?

  • The integration of _CUSTOM_ALLOWED_TYPES into both find_class and _is_allowed_type in the restricted unpickler.
  • The lazy-loading export structure in __init__.py.

Related Issue

Fixes #7413

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue.

Copilot AI review requested due to automatic review settings July 31, 2026 08:48
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 31, 2026

Copilot AI 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.

Pull request overview

Adds a process-wide registry to extend the restricted checkpoint unpickler’s allowlist so applications in hosted environments can permit custom workflow state types without modifying checkpoint storage configuration.

Changes:

  • Introduces a global _CUSTOM_ALLOWED_TYPES registry and a public register_checkpoint_type(...) API in checkpoint encoding.
  • Extends restricted unpickling checks to permit globally registered types.
  • Exposes register_checkpoint_type from the package root and adds unit tests covering registration and validation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py Adds global allowlist + registration API and integrates it into restricted unpickling.
python/packages/core/agent_framework/init.py Re-exports register_checkpoint_type via the lazy export mechanism and __all__.
python/packages/core/tests/workflow/test_checkpoint_unrestricted_pickle.py Adds tests validating that global registration allows restricted deserialization.
Suppressed comments (1)

python/packages/core/tests/workflow/test_checkpoint_unrestricted_pickle.py:552

  • This test also mutates the process-wide checkpoint type registry and should restore it to avoid leaking global state to other tests.
def test_register_checkpoint_type_allows_string_key():
    """Custom types registered globally by string key are allowed during restricted deserialization."""
    from agent_framework import register_checkpoint_type

    original = _GloballyAllowedStringState(value="globally_allowed")

Comment thread python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Mahajan-Sachin

Copy link
Copy Markdown
Author

@copilot please review

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

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Allow devs to register custom types for checkpoint serialization and deserialization

2 participants