AI-Assisted Coding · Module 2Lesson 3

Study explanation · Lesson 03 · 15 min

One endpoint per prompt

The API comes alive one route at a time: POST, GET list, GET by id, PATCH, DELETE, each from its own strict prompt and each verified with curl or Swagger before the next. A vague-versus-strict experiment on POST shows why.

Listen to this lesson

Hear the lesson read aloud, with each sentence highlighted as you go.

Narrated by an AI voice.

One endpoint, one verification

This is where the API comes alive, and the teaching rule is simple: one endpoint per prompt, one verification before moving on. Asking for all the endpoints at once makes the output harder to review and lets mistakes multiply. Each route in Part 2.2 has its own prompt in the library (B1, then B2.1 to B2.4), and each one says “Add ONE route”, names the three context files, and ends with a DO-NOT list that includes leaving every other route alone.

Key ideaVague prompts can create plausible code. Strict prompts create code you can verify.

The prompt quality experiment

The demo first submits a vague prompt for POST /tasks and does not apply the result. The point is to review what vague prompting misses. Then the strict prompt is submitted, in a fresh chat, with the relevant files attached. The difference is not that the vague output is obviously broken; it is that you have nothing exact to check it against.

Vague versus strict prompting for POST /tasks · Lecture notes p. 5

Vague

  • Write a POST /tasks endpoint using FastAPI.
  • May return 200 instead of 201
  • May invent models or miss imports
  • May create a new FastAPI app

Strict

  • Attach models and storage
  • POST /tasks -> 201, response model, imports
  • A DO-NOT list
  • Code you can inspect and verify against exact expectations

Record what the vague prompt missed and what the strict one fixed: that comparison is a deliverable.

Part of Prompt B1 (strict version)Exact decorator and signature: @app.post("/tasks", response_model=TaskResponse, status_code=status.HTTP_201_CREATED, tags=["tasks"]) def create_task(payload: TaskCreate) -> TaskResponse: ... DO NOT: - DO NOT create a new FastAPI() instance. - DO NOT generate UUIDs or timestamps in the route; storage handles that. - DO NOT add manual request validation; rely on Pydantic. - DO NOT add try/except around storage.add_task. - DO NOT add any other route.

Endpoint build order

Build the five routes in this order. Each row says what the route does and what its verification has to show.

Endpoint build order · Lecture notes p. 6
OrderEndpointExpected behaviorVerification focus
1POST /tasksCreate a task and return 201.Valid body returns 201; invalid body returns 422.
2GET /tasksReturn a list of tasks. Optional status and priority filters.Empty or no-match list returns 200 with [].
3GET /tasks/{task_id}Return one task if found.Found returns 200; missing id returns 404.
4PATCH /tasks/{task_id}Partially update an existing task.Found returns 200; missing id returns 404; invalid payload returns 422.
5DELETE /tasks/{task_id}Delete an existing task.Existing task returns 204 with no body; missing id returns 404.

Command from the notesuvicorn app.main:app --reload --port 8000 # Verify each route with curl, then open http://localhost:8000/docs to inspect Swagger UI.

Review checklist before applying each route

Before you click Apply in Editor on any route, read it against these six points.

  • Uses the existing FastAPI app; it does not create a new FastAPI() instance.
  • Uses the existing models and storage helpers; it does not invent new helper names.
  • Uses the exact status code expected for that route.
  • Uses response_model where appropriate.
  • Does not add transition business logic to PATCH yet; that belongs in Part 2.3.
  • Preserves Pydantic v2 patterns.

Read with carePrompt B2.1 writes the optional filters as “TaskStatus | None = None”. That union syntax needs Python 3.10 or later. The author’s own Module 2 log records the app crashing on startup because the project’s environment ran Python 3.9; the review caught it only because the server was actually started. This is an observation from the author’s work, not from the notes.

Common AI mistakes in endpoints

The notes’ list of endpoint mistakes is mostly about status codes and invented names. Each one produces something that looks reasonable in the editor and only shows up when you run curl or open Swagger.

  • POST returns 200 instead of 201.
  • DELETE returns JSON even though 204 should have an empty body.
  • GET /tasks returns 404 for an empty filter result instead of 200 with an empty list.
  • The assistant hallucinates storage.filter or storage.find_one even though storage.py does not define those.
  • The assistant calls .dict() instead of .model_dump() in Pydantic v2 code.
  • The assistant wraps too much code in try/except and hides FastAPI and Pydantic validation errors.

When a route fails its check

Prompt B3 is the targeted correction for this part. It asks you to paste four pieces of evidence: the method and path, the command or Swagger action, the expected status or body, and the actual one. It then limits the fix to the responsible route, preserves the app instance and every other route, insists on the existing helper names, and forbids try/except as a workaround. The weak version, “My endpoint is returning the wrong thing. Fix it.”, gives the assistant no evidence and invites a broad rewrite of app/main.py.

Key ideaExpected versus actual is the smallest useful bug report you can give an assistant.

What you leave Part 2.2 with

Part 2.2 ends with five verified routes, an end-to-end run and the first documentation deliverable.

  • Five working CRUD endpoints, added one at a time.
  • curl or Swagger verification for every endpoint.
  • An end-to-end sequence: create, read, update, list filtered, delete, confirm missing.
  • A prompt comparison log showing what the vague POST prompt missed and what the strict prompt fixed.

Key takeaways

  • One endpoint per prompt, one verification before moving on.
  • POST is 201, an empty list is 200 with [], a missing id is 404, a successful DELETE is 204 with no body.
  • Review every route for a new FastAPI() instance, invented helpers, wrong codes and early business logic.
  • When a route fails, give the assistant expected versus actual and limit the fix to that route.

Built from the Module 2 lecture notes and prompt library; page references are in the source notes below.

Your study checkpoint

Ready to continue?

Mark the lesson complete once you have read it and tried its idea on something real.

Next · Lesson 4