System Messages for Guiding Code Output in Azure OpenAI
Your company wants to use Azure OpenAI to generate code snippets. You need to ensure the generated code does not contain security vulnerabilities. Which approach should you take?
Quick Answer
A system message is the right tool here because it sets standing behavioral instructions that persist across every completion the model generates in that session, rather than needing to be repeated or enforced per request. Instructing the model in the system message to follow secure coding practices — avoiding patterns like SQL injection risks or hardcoded credentials, in line with something like the OWASP Top 10 — shapes how it approaches every subsequent code-generation request without requiring retraining the model or building a separate post-processing step to catch vulnerabilities after the fact. This makes it the most direct and scalable lever available at inference time: no additional infrastructure, no fine-tuning pipeline, just a persistent instruction the model treats as governing context for the whole conversation. It's worth contrasting this with few-shot examples, which are better suited to shaping output style or format through demonstration, and with output-side filtering, which catches problems after generation rather than steering the model away from them in the first place. Any scenario asking for a scalable way to influence how the model behaves across all its outputs, without retraining, is pointing at a system message.
⚠ Common exam trap
Microsoft often tests the misconception that content filters can handle security vulnerabilities, but content filters are for policy compliance (e.g., hate speech, violence), not for code security flaws like injection attacks or improper error handling.
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
✓
Include a system message that instructs the model to generate secure code following best practices.
Azure OpenAI's system messages allow you to set the context and behavior of the model, including instructing it to follow secure coding best practices (e.g., OWASP Top 10). This is the most direct and scalable way to influence the model's output at inference time without requiring post-processing or retraining. The system message acts as a persistent instruction that guides every response, making it the recommended approach for steering content generation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Scan the generated code with a static analysis tool after generation.
Why it's wrong here
Post-processing is good but not the primary approach for prevention.
- ✓
Include a system message that instructs the model to generate secure code following best practices.
Why this is correct
System messages can guide the model to produce secure code.
- ✗
Use content filters to block malicious code.
Why it's wrong here
Content filters are not designed to detect security vulnerabilities.
- ✗
Fine-tune the model on a dataset of secure code examples.
Why it's wrong here
Fine-tuning may help but requires significant effort.
Go deeper
Related to this question
About these practice questions
Courseiva writes every AI-102 question from scratch — 945 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
6 more ways this is tested on AI-102
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A company uses Azure OpenAI to generate code snippets. They notice that the model sometimes produces code that uses deprecated APIs. They want to minimize this without retraining the model. What should they do?
medium- A.Fine-tune the model on a dataset of recent code.
- B.Set the temperature parameter to 0 to reduce randomness.
- ✓ C.Add a system message instructing the model to use only current, non-deprecated APIs.
- D.Provide a few-shot example of correct code in the prompt.
Why C: Adding a system message in Azure OpenAI allows you to set high-level instructions that guide the model's behavior without retraining. By explicitly instructing the model to use only current, non-deprecated APIs, you leverage the system prompt's ability to influence output style and content, effectively reducing deprecated API usage in generated code snippets.
Variation 2. A developer uses the Azure OpenAI SDK to generate code snippets. The generated code sometimes contains security vulnerabilities. What is the most effective way to mitigate this risk?
medium- A.Set the temperature parameter to 0 to make the output deterministic.
- B.Post-process the generated code using a static code analysis tool.
- C.Fine-tune the model on a dataset of secure code examples.
- ✓ D.Include a system message that instructs the model to avoid insecure coding patterns.
Why D: A system message sets the behavioral context for the model at inference time, instructing it to avoid insecure coding patterns without requiring retraining. This is the most direct and effective mitigation as it leverages the model's instruction-following capability to reduce vulnerabilities in generated code, aligning with Azure OpenAI's content filtering and safety system guidance.
Variation 3. A developer uses the Azure OpenAI API to generate code. They want to ensure that the generated code is in Python. Which parameter should they set?
medium- A.temperature
- B.top_p
- ✓ C.system message
- D.max_tokens
Why C: The system message is used to set the behavior and context of the AI model, including specifying the desired output format or language. By setting the system message to 'You are a helpful assistant that always writes code in Python', the developer can instruct the model to generate Python code consistently. This parameter is part of the chat completions API and directly influences the model's persona and constraints.
Variation 4. A developer uses the Azure OpenAI API to generate code. They want to ensure that the generated code is in Python. Which parameter should they set?
medium- A.temperature
- B.max_tokens
- ✓ C.system message
- D.top_p
Why C: The system message parameter in the Azure OpenAI API is used to set the behavior and context of the assistant, including specifying the desired output format or language. By setting the system message to something like 'You are a helpful assistant that always generates Python code,' the developer can instruct the model to produce Python code consistently. This is the correct parameter for guiding the model's response style and content.
Variation 5. You are using Azure OpenAI Service to generate code snippets for a development team. You notice that the generated code sometimes contains security vulnerabilities. You need to minimize the risk of generating insecure code while maintaining productivity. What should you do?
medium- ✓ A.Use system messages to instruct the model to prioritize security
- B.Fine-tune the model on a dataset of secure code
- C.Set the temperature parameter to 0
- D.Disable content filtering to allow more flexibility
Why A: System messages in Azure OpenAI Service allow you to set the context and behavior of the model, including instructing it to prioritize security when generating code. This approach directly influences the model's output without requiring retraining or sacrificing flexibility, making it the most effective way to reduce security vulnerabilities while maintaining productivity.
Variation 6. You are a generative AI engineer at a financial services company. The company uses Azure OpenAI Service to generate investment summaries. You have deployed a GPT-4 model with a content filter set to 'Low' for hate speech. The model frequently generates summaries that include biased language against certain demographics. You need to reduce biased outputs while maintaining the ability to generate detailed financial analysis. You cannot afford to retrain the model. You have the following options: A) Change the content filter severity to 'High' for all categories, B) Add a system message instructing the model to avoid bias and provide examples of unbiased summaries in the prompt, C) Use the Azure AI Language service to detect bias in the output and regenerate if bias is found, D) Deploy a different model like GPT-3.5 which has less bias. Which course of action should you take?
hard- A.Use the Azure AI Language service to detect bias in the output and regenerate if bias is found.
- ✓ B.Add a system message instructing the model to avoid bias and provide examples of unbiased summaries in the prompt.
- C.Change the content filter severity to 'High' for all categories.
- D.Deploy a different model like GPT-3.5 which has less bias.
Why B: Adding a system message and examples in the prompt (few-shot learning) can effectively reduce biased outputs without retraining. Option A is wrong because increasing content filter severity may block legitimate financial analysis content. Option C is wrong because detecting bias after generation and regenerating is inefficient and may still produce biased output. Option D is wrong because GPT-3.5 may also exhibit bias and may not provide the same quality of financial analysis.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This AI-102 practice question is part of Courseiva's free Microsoft 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 AI-102 exam.