You run pip install "django==4.2" "celery>=5" and, a minute later, pip stops with ResolutionImpossible and a wall of text that begins “The conflict is caused by”. That message was not written by pip. It is raised by resolvelib, the small backtracking resolver that pip vendors and drives through its own provider object. Every pin, every rejected candidate, and every “backtracking to” line you have ever scrolled past originates in a few hundred lines of generic code that knows nothing about Python versions or indexes.
If you have tried to read resolvelib expecting one resolvers.py, you probably landed on a write-up from 2019. The library moved on. The code now lives under src/resolvelib/resolvers/, the provider contract is split from the driver, and optimistic backjumping has been layered on top of the original depth-first search. This tour walks the tree as it exists on main today rather than repeating the old map.
Everything below is verified against a fresh clone at version 1.2.2.dev0: nine Python modules and roughly 1,060 lines, with zero runtime dependencies and requires-python >= 3.9. The repository is tiny, but it encodes a surprising amount of packaging folklore. The aim is to make the failure message you already saw legible, line by line, so the next time pip refuses to install something you can tell exactly which phase gave up.
The public Resolver wires an AbstractProvider and a BaseReporter into a Resolution object, which owns a stack of State values and drives pinning and backjumping.
Reading the overview from left to right:
Resolveris the facade that runs aResolutiononce and turns the final state into aResult, at Resolver, L569.Resolution.resolveis the round loop that pins one name per iteration, at Resolution.resolve, L418._attempt_to_pin_criterionwalks a criterion’s candidates in preference order and stops at the first workable one, at _attempt_to_pin_criterion, L214._get_preferenceforwards a sort key to the provider so the next name to pin is the provider’s choice, at _get_preference, L181._backjumprewinds the state stack and re-applies the incompatibilities it learned, at _backjump, L305._patch_criteriarefetches matches for a broken name while excluding candidates known not to work, at _patch_criteria, L251.AbstractProvider.find_matchesis the single call that is allowed to see an index, at AbstractProvider.find_matches, L92.build_iter_viewwraps whatever the provider returns so candidates can be iterated more than once, at build_iter_view, L200.
The detailed view expands the same flow into 33 nodes and 7 groups, separating the driver, candidate handling, backtracking bookkeeping, data structures, provider contract and failure signals.
The Provider Contract Carries the Weight
resolvelib deliberately refuses to know what a package is. AbstractProvider in providers.py defines five abstract methods that each raise NotImplementedError: identify maps a requirement or candidate to a stable key; find_matches returns preferred candidates minus known incompatibilities; is_satisfied_by checks one candidate against one requirement; get_dependencies lists what a candidate asks for; and get_preference produces a sort key. A sixth, narrow_requirement_selection, is optional: its default returns the identifier set unchanged, so a provider that ignores it still works. The five stubs are why the library has no runtime dependencies at all.
That split is the reason the same engine can drive Python wheels, CocoaPods pods, and Swift packages. The repository ships three functional harnesses under tests/functional: python, cocoapods, and swift-package-manager, each with its own provider that translates a different ecosystem into these five verbs. The generic code never sees a version string. pip supplies the Python meaning by pairing resolvelib with packaging for specifier and version semantics, and that division is why a change to PEP 440 handling never touches the resolver.
The second delegate is BaseReporter, eight optional pass methods that fire at named points: starting, starting_round, ending_round, ending, adding_requirement, resolving_conflicts, rejecting_candidate, and pinning. If you subclass it and log inside rejecting_candidate, you will see exactly the candidates the resolver gave up on, in the order it tried them. That is the cleanest way to observe backtracking without editing the library.
Where a Round Spends Its Time
The loop in Resolution.resolve begins by computing unsatisfied_names: every key whose criterion has information but whose current pin does not satisfy all of that criterion’s requirements. If the list is empty, resolution is finished and ending fires. Otherwise the loop chooses one name, attempts to pin it, and pushes a fresh state before the next round.
Choice is a two-stage filter. If more than one name is unsatisfied, the resolver first asks the provider to narrow the set through narrow_requirement_selection. It then calls min(narrowed_unstatisfied_names, key=self._get_preference) at L510, so the provider’s sort key breaks every tie. When only one name survives narrowing, the resolver skips the sort entirely, which matters because get_preference is documented as being called O(n) times per backtrack step.
The max_rounds argument defaults to 100 in the Resolver.resolve signature at L572. Exceeding it raises ResolutionTooDeep, the exception you see when a circular dependency or a very deep transitive graph keeps the search alive past the budget. A typical pip run never approaches that limit; a pathological lockfile can.
State is cheap on purpose. A State is a named tuple of mapping, criteria, and backtrack_causes, and _push_new_state at L96 copies the mapping and the criteria dict while slicing the causes list, so each round works on an isolated snapshot. When a pin invalidates names that were previously satisfied, resolve recomputes those names and calls _remove_information_from_criteria at L154, which strips the stale requirement sources so later rounds do not chase dependencies that no longer apply.
From Requirements to a Pinned Graph
This is the part worth reading with the file open. Four mechanisms turn a bag of requirements into a dependency graph.
Criteria accumulate candidates. _add_to_criteria at L110 takes one requirement, asks the provider for its identifier, and refetches matches. It does not query the provider with a single requirement. It wraps the whole criteria mapping in two IteratorMapping objects — one projecting each criterion through iter_requirement, one through incompatibilities — and appends the new requirement to the entry for that identifier. The provider therefore sees every requirement currently attached to the name, plus everything already ruled out. The returned matches are wrapped with build_iter_view, then stored as Criterion(candidates=..., information=..., incompatibilities=...) at L145. If the resulting candidate view is empty, RequirementsConflicted is raised immediately at L150, carrying the offending criterion.
The candidate view is worth a note. build_iter_view at L200 returns _FactoryIterableView when the provider hands back a callable, and _SequenceIterableView otherwise, materialising non-sequences into a list. The factory variant calls itertools.tee in __iter__ at L176 so the same candidates can be walked more than once; the resolver relies on that when it re-enters a criterion after a backtrack. Without the wrapper, a provider that returned a bare generator would be consumed by the first pass and appear empty on the second.
Pinning moves a candidate to the end. _attempt_to_pin_criterion iterates the criterion’s candidates. For each one it calls _get_updated_criteria, which copies the criteria dict and replays get_dependencies(candidate) through _add_to_criteria. If that raises RequirementsConflicted, the candidate is reported through rejecting_candidate and added to the causes list. On success the resolver re-checks is_satisfied_by against every requirement to catch faulty providers, fires pinning, and then does something subtle: self.state.mapping.pop(name, None) followed by self.state.mapping[name] = candidate at L242–243. The newly pinned candidate is deliberately moved to the end of the mapping, because backtracking later reads the last item to decide what to unwind.
Preference decides the order. Because _get_preference simply forwards resolutions, candidates, information, and backtrack_causes, the ordering policy lives entirely in the host. pip ranks names by how constrained they are and how recently they caused a conflict; a different provider could rank by popularity. The generic driver is indifferent.
Backjumping rewinds state. When every candidate fails, _extract_causes deduplicates the RequirementInformation from the failed criteria, and resolve calls _backjump. Inside, the first move is del self._states[-1] at L343, discarding the state that could not be pinned. It then pops states from the end in a loop, calling broken_state.mapping.popitem() at L351 to recover the name and candidate that were most recently pinned. It collects every incompatibility recorded in that state at L392–394 and appends the candidate that just failed at L397. Finally it pushes a new state and calls _patch_criteria, which reruns find_matches for each affected name with the broken candidates excluded; if any name still has no candidates, patching returns False and the loop backjumps again.
Backjumping Without Copying Everything
The naive version of the above pops one state at a time and re-pins immediately. The current code adds optimistic backjumping, guarded by _OPTIMISTIC_BACKJUMPING_RATIO = 0.1 at L35. The idea is to jump further than the strictly safe point when the current candidate’s dependencies overlap the incompatible dependencies, on the bet that the real cause is higher up the stack. Before taking that bet the resolver snapshots the whole state list in _save_state, and if the gamble produces no progress it calls _rollback_states, which restores the snapshot and sets the ratio to 0.0 so the safe path takes over.
The cutoff arithmetic lives in the round loop at L454–456: the remaining rounds are multiplied by the ratio to get a budget, and if that budget is spent the rollback fires. This is why the CHANGELOG for 1.2.1 mentions an extra guard against exhausting states during backjumping. The heuristic exists because dependency conflicts are rarely local, but a wrong jump is expensive, so the engine keeps a way back.
Honest Limits
resolvelib is a search engine, not an installer. It never downloads, never parses a version, and never touches a wheel. If your provider returns candidates in the wrong order, or reports dependencies inconsistently, the resolver will happily produce a wrong answer; the InconsistentCandidate check at L235 only catches the narrow case where a candidate fails its own requirements.
The search is single-threaded and has no parallelism. In the worst case backtracking is exponential, which is why the library leans on provider preference and optimistic jumps rather than exhaustive enumeration. max_rounds is a hard ceiling, not a performance guarantee, and raising it trades memory for time. The public surface is intentionally small, but it is also unforgiving: an incomplete narrow_requirement_selection that returns an empty set raises RuntimeError rather than falling back. The three functional harnesses exercise the engine against real CocoaPods, Python, and Swift fixtures, with dedicated cases such as backjump-test-1 through backjump-test-4 under tests/functional/python/inputs/case/.
Finally, most tutorials you will find still describe a single Enjoyed this post? Never miss out on future posts by following us resolvers.py and a factory.py that does not exist in this tree. The Resolver class lives in resolution.py; criterion.py holds a plain immutable bundle with no merge helper. If you are comparing resolvers, read poetry or uv alongside this one — they make different trade-offs between exactness and speed — but keep pip in mind as the reason this tiny engine exists at all.