Skip to main content
LearnMLAcademy
Intermediate10 min read

Prompt Engineering, Structured Outputs & Function Calling

Design production prompts with few-shot examples and output schemas, with token walkthroughs, prompt design tasks, and retrieval exercises.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Why does one prompt work better than another?

Weak: “Tell me about this.”

Clearer: “Summarize the following article in five bullet points for a first-year college student. Keep each bullet under 20 words.”

The second request is easier to follow because the task, audience, constraints, and desired shape of the answer are visible. Prompt engineering is the practice of making those instructions clear, testable, and appropriate for the job.

A practical four-part prompt

Prompt anatomy

Prompt anatomy
PartQuestion it answers
TaskWhat should the model do?
ContextWhat information should it use?
ConstraintsWhat rules or limits should it follow?
Output contractWhat shape should the answer have?
A structured promptConceptual
TASK
Summarize the article below.

CONTEXT
<article>
...article text...
</article>

CONSTRAINTS
- Audience: first-year college student
- Exactly 5 bullets
- Each bullet under 20 words

OUTPUT CONTRACT
Return JSON: { "bullets": ["..."] }
The tags are just a clear delimiter convention; they are not magic syntax.

Read this prompt as an application contract: the task says what to do, the context supplies relevant information, the constraints limit acceptable behavior, and the output contract makes the result easier to check. None of those instructions substitutes for checking the result.

Zero-shot, one-shot, and few-shot examples

Zero-shot means giving the instruction without examples. One-shot gives one example of the desired input/output pattern. Few-shot gives a small set of examples.

Structured output gives shape — not truth

If software needs a list of objects such as {name, due_date}, define a schema and validate the output. A parser can prove that required fields and types are present. It cannot prove that a date is factually correct, authorized, or acceptable under business rules.

  1. Generate

    Ask for the defined structure.

  2. Parse

    Reject malformed syntax or invalid types.

  3. Validate semantics

    Check allowed values, business rules, permissions, and evidence.

  4. Handle failure

    Repair, ask for clarification, retry safely, or escalate according to policy.

Tool or function calling is a proposal, not permission

An LLM can propose a tool name and arguments, but application code should validate them before execution. If the model proposes get_weather(city="Pune", unit="Kelvin") and the schema allows only C or F, the application should reject or repair the arguments according to its policy.

The model proposes; trusted application code validates and executes

User

LLM

proposes tool + arguments

Validator

schema · identity · authorization

Trusted tool

application executes

Result → LLM → User

structured result supports response

Reject or request correction

invalid or unauthorized arguments never reach the tool

Tool arguments must pass schema, authorization, range, and side-effect checks before trusted code runs anything.

Debug prompts like software requirements

If two instructions conflict—such as “return only JSON” and “explain your reasoning in a paragraph”—first remove or reconcile the conflict and retest the same cases. Adding unrelated examples or immediately fine-tuning the model hides the real problem.

A good prompt can improve how clearly the model understands the task. It cannot magically give the model a current private policy, a missing database record, or guaranteed facts.

Why the next lesson matters

Prompting changes the request but not the model's learned parameters. How were those parameters learned in the first place?

Pretraining & Fine-Tuning →

Summary / Key Takeaways

  • A useful prompt makes the task, context, constraints, and output contract explicit.
  • Examples can clarify a desired pattern, but they must not conflict with the written instructions.
  • Structured output makes the shape predictable; separate semantic and business validation is still required.
  • Tool calls are model proposals that application code must validate and authorize.
  • Prompt debugging should remove ambiguity and conflicting constraints before changing unrelated components.
  • Prompting changes the input to a trained model; it does not retrain the model or automatically provide missing knowledge.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Prompt Engineering, Structured Outputs & Function Calling"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Knowledge Check

Progress: 0/5

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 5conceptual
What four elements make a prompt easier to test?
Question 2 of 5interpretation
A response parses as JSON and matches the schema. What has NOT yet been proven?
Question 3 of 5debugging/diagnostic
A prompt contains two conflicting formatting rules and the model alternates between them. What is the best first debugging move?
Question 4 of 5application/scenario
The model proposes `get_weather(city="Pune", unit="Kelvin")`, but the tool schema permits only C or F. What should happen?
Question 5 of 5practical selection
A workflow needs software to consume a predictable list of `{name, due_date}` objects. What is the best starting output design?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1What four elements make a prompt easier to test?▼

Correct Answer: Task, context, constraints, and output contract

Clear task definition, relevant evidence/context, explicit constraints, and an output contract make behavior measurable.

2A response parses as JSON and matches the schema. What has NOT yet been proven?▼

Correct Answer: That the values are factually supported and allowed by business rules

Structural validation does not prove semantic truth, authorization, or business validity.

3A prompt contains two conflicting formatting rules and the model alternates between them. What is the best first debugging move?▼

Correct Answer: Remove or reconcile the conflicting constraints and retest on the same cases

Prompt debugging should isolate the actual ambiguity/conflict before changing unrelated components.

4The model proposes `get_weather(city="Pune", unit="Kelvin")`, but the tool schema permits only C or F. What should happen?▼

Correct Answer: Reject or repair the arguments through the defined validation policy

Application code owns schema validation and execution; model proposals are not trusted commands.

5A workflow needs software to consume a predictable list of `{name, due_date}` objects. What is the best starting output design?▼

Correct Answer: A defined structured-output schema plus semantic/business validation

A schema provides predictable fields/types, while additional validation checks their meaning and correctness.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Next