Generative AI Leader Practice Question: Techniques to Improve Generative AI Model Output
A developer uses a code generation model to write Python functions. The output frequently contains syntax errors due to incorrect braces and indentation. Which technique should be used to produce syntactically valid code?
⚠ Common exam trap
A common misconception is that fine-tuning or prompt engineering alone can guarantee syntactic correctness, but only constrained decoding (or grammar-guided generation) provides a hard guarantee against syntax errors by actively restricting the output space to valid tokens per the language's grammar.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Apply constrained decoding techniques that enforce a grammar for the target programming language.
Constrained decoding (also called grammar-guided generation) enforces the syntax rules of the target language (e.g., Python) during token generation by restricting the model's output to only valid tokens according to a formal grammar (e.g., EBNF or context-free grammar). This directly prevents syntax errors like incorrect braces or indentation, which are structural, not semantic, issues. Techniques such as using a parser-based logit processor or a constrained beam search ensure every generated token sequence is syntactically valid.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Increase the temperature to introduce more varied token choices.
Why it's wrong here
Raising temperature widens the sampling distribution, so low-probability tokens — including malformed brackets and stray indentation — become likelier, worsening the syntax errors. Temperature is a creativity control for brainstorming or varied prose, where diverse outputs are wanted; it cannot enforce Python's grammar, which requires constrained decoding or validation.
- ✓
Apply constrained decoding techniques that enforce a grammar for the target programming language.
Why this is correct
Constrained decoding masks tokens that would violate a Python grammar, so braces and indentation are enforced at generation time rather than corrected afterwards. This directly satisfies the stem's requirement for syntactically valid output, unlike prompt engineering or post-hoc linting, which cannot guarantee validity during sampling.
- ✗
Fine-tune the model on a large corpus of syntactically correct Python code.
Why it's wrong here
Fine-tuning adjusts weights toward corpus patterns but offers no guarantee of syntactic validity at inference; malformed output remains possible. It suits teaching domain vocabulary, tone or specialised tasks, not enforcing grammar that a constrained decoder handles deterministically.
- ✗
Provide a few-shot example of correct Python function in the prompt.
Why it's wrong here
Few-shot examples demonstrate formatting conventions but do not constrain token generation, so malformed braces and indentation can still be emitted. Prompting is for steering style and task shape when the model already produces valid syntax but needs guidance on structure or intent.
Go deeper
Related to this question
About these practice questions
This Generative AI Leader question is part of Courseiva's 1,008-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This Generative AI Leader practice question is part of Courseiva's free Google Cloud certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Generative AI Leader exam.