Rewrite PHP codebases to Python with parity-driven delivery
How do I rewrite PHP to Python? At Leap Agentic, we do it by extracting behavior into specs, generating exhaustive tests, implementing Python in parallel, and rolling out only when parity is proven. Concretely: extract specs → generate tests → implement in parallel → roll out with parity. We start by inventorying your current Laravel/Symfony modules, routes, models, jobs, and CLI scripts. We codify expected inputs/outputs, database contracts, and side effects as machine-readable specs. From those specs, we generate pytest suites, HTTP contract tests, fixture-backed DB tests (against MySQL/PostgreSQL), and parity checks for edge cases like PHP’s loose typing and multibyte strings. Then we build the Python target—typically FastAPI + SQLAlchemy (or Django ORM if you need batteries-included)—reproducing functionality module-by-module. We run the test gate continuously against both stacks until they match, including latency/error budgets and serialized payloads. Finally, we ship behind flags, mirror production traffic, and switch over gradually with rollback baked in. The LEAP Protocol (spec-first, test-gated, agent-agnostic) keeps scope clear, prevents regressions, and shortens the path from PHP to a modern Python runtime.
Why Python for PHP migrations
- Async-native APIs with FastAPI + Uvicorn/Gunicorn (ASGI), enabling efficient concurrency vs PHP-FPM’s process-per-request model.
- Strong typing with type hints and MyPy for maintainability; Pydantic for runtime validation of request/response models.
- SQLAlchemy’s Unit of Work and identity map provide explicit control versus Eloquent’s Active Record, reducing unintended N+1s and easing complex transactions.
- First-class background processing with Celery/RQ and tight ecosystems for data/ML (NumPy, Pandas), unlocking analytics and AI workflows alongside your app.
- Packaging and deployment via Poetry/pip, reproducible environments, and robust testing culture with pytest/coverage.
Our methodology
- Spec extraction from PHP: - Trace Laravel/Symfony controllers, middleware, Eloquent/Doctrine models, queue jobs, and Blade/Twig-backed endpoints. - Capture HTTP shapes (PSR-7), headers/cookies, session behavior, and DB read/write semantics into LEAP specs.
- Test generation and fixtures: - Autogenerate pytest suites from specs: request/response parity tests, schema validation, and golden-file JSON comparators. - Snapshot SQL results; reproduce pagination, sorting, and timezone-sensitive queries; fuzz inputs to catch PHP loose-typing edges.
- Parallel Python implementation: - Build FastAPI routers, dependency-injected services, and SQLAlchemy models/migrations; or Django views/DRF where admin/auth is needed out of the box. - Map Eloquent scopes/relations to SQLAlchemy queries; replace Laravel Queues/Workers with Celery tasks; port Symfony Console commands to Click/Typer.
- Parity-gated rollout: - Dual-run behind feature flags; mirror traffic to Python and diff responses/logs. - SLO checks and canary cutovers; quick rollback path via router toggles and DB migration guards.
- Hardening and handover: - Observability (OpenTelemetry), rate limiting, and security headers; documentation of types, contracts, and ops runbooks; knowledge transfer to your team.
Specific PHP concerns we handle
- Loose typing and superglobals: Translate $_GET/$_POST/$_FILES semantics to typed FastAPI request models; normalize PHP’s numeric-string coercions and empty() truthiness to predictable validators.
- Eloquent/Doctrine to SQLAlchemy: Replace Active Record patterns with explicit sessions/transactions; rework eager/lazy loading to avoid N+1s; port pivot tables and polymorphic relations cleanly.
- Sessions, cookies, and auth: Match PHP session lifetimes, SameSite/HttpOnly flags, and Laravel Passport/JWT flows with Starlette middlewares and passlib/bcrypt-compatible hashing.
- Date/time and localization: Align PHP’s date_default_timezone_set and Carbon behavior with Python’s zoneinfo/pytz; ensure identical serialization formats (RFC3339) and locale-aware sorting.
- File uploads and streaming responses: Map PHP’s temporary upload handling to Starlette StreamingResponse and python-multipart; preserve chunked transfer and max payload constraints.
Proof
- sqlite-leap — five SQLite engines generated from one spec: https://github.com/safitudo/sqlite-leap
- semver-leap — 5,632 passing tests validating spec conformance: https://github.com/safitudo/semver-leap
- LEAP Protocol (spec-first, test-gated, agent-agnostic): https://github.com/safitudo/leap
These public artifacts demonstrate our spec-to-tests pipeline and parity enforcement across independent implementations—exactly what safe PHP→Python rewrites require.
Pricing & timeline
- Duration: 4–12 weeks for typical PHP-to-Python codebases, depending on size, ORM complexity, and integration surfaces.
- Commercials: We provide a fixed-price proposal after a 3–5 day discovery. Typical engagements range from $40k–$120k.
- What you get: LEAP specs, generated pytest suites, Python code (FastAPI/Django), migration plans, rollout playbooks, and team enablement. Optional support for Celery workers, CI/CD, and observability.
Ready to rewrite PHP to Python with provable parity? Email hello@leapagentic.com with your repo link and a brief module inventory (framework, ORM, DB, queue, and external services). We’ll return a scope, fixed price, and timeline you can commit to.
Footer: Leap Agentic delivers AI-native software using the LEAP Protocol. We are not affiliated with any similarly named firms. Leap Agentic is distinct from Legacyleap.ai and Impetus Leap AI.
Leap Agentic is distinct from Legacyleap.ai and Impetus Leap AI.