The Contract Nobody Enforces
API specifications, particularly OpenAPI (formerly Swagger), are intended to be the single source of truth. They define the contract between API producers and consumers. From this contract, developers generate SDKs, frontend types, API documentation, and mock servers. The promise is that what the spec says is what the API does. However, in practice, this contract is often broken. The implementation code drifts away from the spec, leading to subtle but critical integration failures.
Consider a scenario where a generated client attempts to call DELETE /items/{id}. If the server implementation has been updated and now returns a 405 Method Not Allowed because that endpoint was changed or removed, the entire integration breaks. This isn't a theoretical problem; it's a common pain point in software development where API definitions and their actual implementations diverge over time. The generated artifacts—SDKs, types, docs—become outdated, silently lying to developers and systems relying on them.
This is precisely the problem Sunny Dachs, a software engineer, set out to solve with oas-drift. It's a command-line interface (CLI) tool designed to detect this spec-to-code drift. What makes oas-drift notable is its simplicity and lack of external dependencies. It's built using only Python's 3.11+ standard library, meaning no heavy frameworks or external packages are required for installation or execution. It also eschews complex solutions like LLMs, focusing on direct code and spec analysis.

Detecting Three Classes of Drift
oas-drift operates by comparing an OpenAPI JSON specification file against a given Python codebase. It identifies three distinct categories of discrepancies:
- SPEC ONLY: These are endpoints or operations defined in the OpenAPI specification but for which no corresponding route or handler exists in the Python codebase. This means features described in the spec are not actually implemented, potentially leading to consumer disappointment or failed requests for non-existent functionality.
- CODE ONLY: Conversely, this category covers routes or handlers that exist in the Python code but are entirely missing from the OpenAPI specification. This is equally problematic, as it means undocumented API behavior is exposed. Consumers relying solely on the spec will be unaware of these endpoints, leading to unexpected errors or security vulnerabilities if these undocumented routes are exploited.
- DRIFT: This is the most granular category. It identifies specific operations (e.g., a particular HTTP method on a given path) that are defined in the spec but do not match the implementation in the code. This could involve mismatches in HTTP methods (e.g., spec says PUT, code implements POST), incorrect path parameters, or missing query parameters that are expected by the spec.
The tool parses both the OpenAPI spec and the Python source files. For the OpenAPI spec, it understands the structure and identifies all defined paths, operations, and HTTP methods. For the Python codebase, it uses Python's built-in Abstract Syntax Tree (AST) module to analyze the code. This allows it to programmatically discover defined routes, often by inspecting frameworks like Flask or FastAPI, without needing to execute the code itself. By cross-referencing these two representations, oas-drift can pinpoint where the contract has been violated.
How oas-drift Works Under the Hood
The core of oas-drift's functionality lies in its ability to parse and compare structured data from two very different sources: a JSON-based OpenAPI specification and Python source code files. The process begins with loading the OpenAPI specification. The tool expects a valid OpenAPI 3.x JSON file. It then systematically extracts all defined routes, typically represented as paths objects, and within those, the available HTTP methods (GET, POST, PUT, DELETE, etc.) and their associated parameters.
For the Python codebase, oas-drift leverages the `ast` module. This module allows Python programs to parse Python source code into an Abstract Syntax Tree, which is a tree representation of the code's structure. By traversing this AST, the tool can identify function definitions, class structures, and importantly, route decorators or function calls that define API endpoints. For instance, in a Flask application, it might look for routes defined using `@app.route('/path', methods=['GET'])`. For FastAPI, it would inspect path operation decorators like `@app.get('/path')`.
Once both the spec and code structures are represented internally, oas-dist performs a comparison. It iterates through every path and method defined in the spec and checks if a corresponding route exists in the parsed code. If a spec-defined route is missing in the code, it's flagged as SPEC ONLY. If a route is found in the code but not in the spec, it's flagged as CODE ONLY. When a route exists in both, it performs a deeper check on HTTP methods and parameters to flag specific operational differences as DRIFT.
The tool’s zero-dependency approach is a significant advantage for adoption. Developers can simply download the script or install it via pip without worrying about conflicting package versions or complex environment setups. The reliance on standard Python libraries makes it robust and predictable across different development environments. This simplicity is key to its utility as a quick, reliable check for API contract adherence.
Why This Matters: The Cost of Drift
The divergence between an API's specification and its actual implementation is more than an annoyance; it's a source of significant technical debt and operational risk. When the spec is the source for generated clients, SDKs, and documentation, any drift means these generated assets become inaccurate. This leads to:
- Broken Integrations: As seen in the initial example, clients calling non-existent endpoints or using incorrect methods will fail. This impacts third-party developers, internal microservices, and frontend applications.
- Wasted Development Effort: Developers spend time debugging issues that stem from outdated or incorrect generated code, assuming the spec is correct. This debugging cycle is inefficient and frustrating.
- Security Vulnerabilities: Undocumented endpoints (CODE ONLY) can expose sensitive functionality that hasn't undergone security review or been documented for proper usage, creating potential attack vectors.
- Poor Developer Experience (DX): Inaccurate documentation and unreliable SDKs lead to a frustrating experience for API consumers, hindering adoption and potentially damaging the API provider's reputation.
oas-drift offers a proactive solution. By integrating checks for spec-code drift into CI/CD pipelines or running them as part of regular development workflows, teams can catch these discrepancies early. This prevents outdated generated code from being deployed and ensures that the OpenAPI specification remains a reliable source of truth. The tool's minimal dependencies mean it can be easily adopted without significant overhead.
The surprising detail here is not the existence of drift—that's common—but the simplicity of the solution offered by oas-drift. Many solutions for API contract management involve complex gateways, runtime analysis, or extensive tooling. oas-drift strips all that away, providing a direct, code-level verification that’s accessible to any Python project.
The Future of API Contract Management
Tools like oas-drift highlight a growing need for robust, automated API contract validation. As microservice architectures become more prevalent and APIs serve as the connective tissue for complex systems, maintaining the integrity of the API contract is paramount. While oas-drift focuses on Python backends, the principles it embodies—strict adherence to a defined contract and automated drift detection—are applicable across all programming languages and API technologies.
What nobody has addressed yet is the long-term strategy for managing spec evolution. While oas-drift catches current drift, the process of updating both the spec and the code in lockstep requires strong organizational processes. How do teams ensure that spec changes are propagated correctly and timely to the implementation, and vice-versa, beyond just automated checks? This remains a critical human and process challenge that even the best tools can only partially solve.
For developers, oas-drift provides an immediate way to shore up their API contract enforcement. By ensuring that the code reflects the specification, they can rely on generated assets and reduce integration headaches. Founders can see this as a way to reduce technical debt and improve the reliability of their core product offerings. Security professionals can use it to identify undocumented endpoints. Ultimately, oas-drift offers a pragmatic, dependency-free approach to a persistent problem in API development.
