rohitg00/ai-engineering-from-scratch · error · ValueError
independent_requests must be positive
Error message
independent_requests must be positive
What it means
Precondition on choose_access_pattern: AccessNeeds.independent_requests counts the separate requests in the workload, and a value below 1 (zero or negative) describes no work at all, so the client/delivery recommendation would be undefined.
Solutions
- Set independent_requests to the actual number of parallel requests (>= 1)
- Treat zero requests as 'nothing to send' and skip calling the chooser
- Validate the AccessNeeds values in a single construction-time check
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:43 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/7587bdb59d4d15f2.
Report an issue: GitHub.
Appendix: source
Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:43
messages: list[dict[str, Any]]
turns: int
@dataclass(frozen=True)
class AccessNeeds:
"""Workload facts used to choose a client and completion pattern."""
supported_sdk: bool = True
custom_transport: bool = False
progressive_output: bool = False
independent_requests: int = 1
can_wait: bool = False
def choose_access_pattern(needs: AccessNeeds) -> dict[str, str]:
"""Choose SDK versus REST separately from sync, stream, or batch."""
if needs.independent_requests < 1:
raise ValueError("independent_requests must be positive")
if needs.progressive_output and needs.can_wait and needs.independent_requests > 1:
raise ValueError("message batches do not provide progressive per-token output")
client = "raw-rest" if needs.custom_transport or not needs.supported_sdk else "sdk"
if needs.can_wait and needs.independent_requests > 1:
delivery = "message-batch"
elif needs.progressive_output:
delivery = "streaming"
else:
delivery = "synchronous"
return {"client": client, "delivery": delivery}
class ScriptedTransport:
"""A stateless transport that returns scripted API responses."""
def __init__(self, responses: list[dict[str, Any]]) -> None:
self._responses = copy.deepcopy(responses)View on GitHub (pinned to 39ea8a1c6d)