Skip to main content

Migration Guide: Container Pattern & Dependency Injection

This guide helps you migrate your code to use the new container pattern and dependency injection.

Overview

We’ve introduced a dependency injection container pattern to replace global singletons, making the codebase more testable and flexible. Benefits:
  • ✅ Easier testing (no global state!)
  • ✅ Multiple independent agent instances
  • ✅ Per-tenant/per-agent configuration
  • ✅ No environment variable pre-seeding in tests
  • ✅ Better type safety and IDE support

What Changed?

Phase 1: Test Infrastructure (✅ Complete)

Before:
After:
Migration Steps:
  1. Use test_container fixture instead of manual setup
  2. Remove environment variable pre-seeding from tests
  3. Use create_test_agent() helper for agent instances

Phase 2: Agent Creation (✅ Complete)

Before:
After:
Migration Steps:
  1. Replace get_agent_graph() with create_agent()
  2. Pass container parameter for full DI benefits
  3. Update tests to use create_test_agent() helper

Backward Compatibility

The old get_agent_graph() function still works:
Deprecation Timeline:
  • Now: New code uses create_agent()
  • v2.0: get_agent_graph() marked as deprecated in docstring
  • v3.0: get_agent_graph() removed

Testing Migration

Old Pattern (❌ Don’t use)

New Pattern (✅ Use this)

Common Migration Patterns

Pattern 1: Test Setup

Before:
After:

Pattern 2: Custom Configuration

Before:
After:

Pattern 3: Multiple Agents

Before:
After:

Helper Functions Reference

Container Helpers

Agent Helpers

Test Helpers

Troubleshooting

Issue: Tests fail with “observability not initialized”

Cause: Old code trying to use global initialization Solution:

Issue: Multiple tests share state

Cause: Using singleton get_agent_graph() Solution:

Issue: Can’t override settings in tests

Cause: Global settings being used Solution:

Best Practices

✅ Do

  1. Use containers in new code
  2. Use test helpers
  3. Create isolated instances
  4. Use fixtures

❌ Don’t

  1. Don’t modify global state
  2. Don’t use singletons in new code
  3. Don’t pre-seed environment variables

Timeline & Rollout

Phase 1 (✅ Complete)

  • Container implementation
  • Test infrastructure migration
  • Helper functions

Phase 2 (✅ Complete)

  • Agent factory functions
  • Test helper updates
  • Documentation

Phase 3 (🚧 Future)

  • Server refactoring
  • Infrastructure layer extraction
  • Complete singleton removal

Questions?

  • See /docs/day-1-developer.md for quickstart guide
  • See /tests/core/test_container.py for examples
  • See /tests/core/test_agent_di.py for agent examples
Need help? Open an issue