Overview

Appendix B. Prompt Engineering Reference

This appendix serves as a compact reference for prompting an AI coding companion effectively when generating SQL, Python, data-cleaning logic, or structured extractions. It emphasizes that results depend heavily on prompt quality, so the goal is to reuse a small set of practical techniques whenever a prompt is vague, inconsistent, or not giving the desired output.

The core idea is that strong prompts often combine four elements: role or context, task, input, and output format. Zero-shot prompting is the default for simple, familiar tasks, while few-shot prompting helps when the format is hard to describe or the model keeps missing the pattern. More advanced approaches include chain-of-thought for multi-step reasoning, self-consistency for checking uncertain answers by repeating prompts, tree-of-thought for exploring multiple solution paths, and role-playing, domain priming, and prompt chaining for shaping behavior and breaking complex work into manageable steps.

The appendix also highlights structured-output prompting as one of the most reliable ways to get machine-usable results, especially by defining schemas with Pydantic and instructing the model to return only what is explicitly present, using null instead of guessing. It closes by warning about common pitfalls such as overengineering, ambiguous instructions, hallucinated fields, fragile regexes or selectors, and output differences across models. The overall message is to treat AI-generated output as a draft: test it, refine it, and add precision wherever the model shows uncertainty.

B.10 A reusable prompt checklist

Before sending a prompt that matters, ask:

  • Did I state the role and context?
  • Did I describe the task specifically, not vaguely?
  • Did I say exactly what output format I want?
  • Did I say where the code will run?
  • For unstructured extraction, did I define a schema and forbid guessing?
  • Would a person, given only this prompt, know what I want?

If the answer to the last question is no, the model will not know either. Add what is missing and try again.

FAQ

What are the four main parts of an effective prompt?

An effective prompt usually has four parts: role/context, task, input, and output format. Role/context tells the model who it should act as and the situation it is in. Task states exactly what you want done. Input is the data or code the task should operate on. Output format specifies exactly how you want the answer returned.

What is zero-shot prompting?

Zero-shot prompting means asking for what you want without giving examples. You rely on the model’s general knowledge. It works best for simple, familiar tasks such as standard SQL joins, basic regex, or well-known APIs and datasets.

When should I use few-shot prompting?

Use few-shot prompting when the format matters and is difficult to describe in words, or when the model keeps guessing wrong. By showing one or more worked examples, you help the model pattern-match to your intent.

What is chain-of-thought prompting used for?

Chain-of-thought prompting asks the model to reason step by step before giving its final answer. It is useful for multi-step problems such as multi-join SQL queries, calculations that depend on earlier steps, or logic that is easy to get subtly wrong.

What is self-consistency prompting?

Self-consistency means running the same prompt multiple times and comparing the answers. If several independent attempts agree, the result is more trustworthy. If they diverge, the task is probably ambiguous or difficult and deserves closer review.

What is tree-of-thought prompting?

Tree-of-thought prompting asks the model to generate several different approaches, evaluate them, and then continue with the most promising one. It is best for open-ended design problems where there is more than one reasonable solution.

How do role-playing, domain priming, and prompt chaining differ?

Role-playing gives the model a persona, such as a senior data engineer reviewing code. Domain priming supplies relevant context up front, such as a schema or glossary. Prompt chaining breaks a big task into smaller prompts and passes each result into the next step.

What is structured-output prompting?

Structured-output prompting uses a defined schema, often a Pydantic model, so the model returns machine-usable data in a predictable shape. The schema and field descriptions become part of the prompt and help control the output format.

How can I reduce hallucinated fields or parameters in model output?

Tell the model to use only what is explicitly present and to return null when it is unsure. If possible, also provide the real documentation or schema. These instructions reduce hallucinated fields and unsupported parameters.

What should I do when the model gives output that is too complex, ambiguous, or inconsistent?

Treat the first response as a draft. Make your environment explicit, such as saying whether the code is for a notebook or a script. Use precise language, ask for the output format you want, and rerun the prompt if results differ across attempts or models.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free