Java Zero to All All questionsPractise free
Interview questions › Build AI Features as a Java Backend Engineer › Start Here — Build Your First AI Feature

What is the difference between a system prompt and a user prompt, and how do you reliably get structured output like JSON out of a model?

Level II · JuniorStart Here — Build Your First AI Feature
Short answer

The system prompt sets durable instructions, role, tone, and constraints that should govern the whole conversation, while user prompts carry the actual per-turn request; keeping instructions in the system prompt makes them harder to override and easier to reuse.

Explanation

To get structured output reliably you do not just ask nicely -- you use the provider's JSON mode or structured-output feature, or supply a function/tool schema, so the model is constrained to emit valid JSON matching a shape. In the Java world Spring AI's entity mapping or a BeanOutputConverter and LangChain4j's typed AiServices let you deserialize directly into a POJO. The senior nuance is that you still validate and handle parse failures, because 'usually valid JSON' is not a contract, and you keep the schema small since every field description is tokens you pay for.

Common mistakes
  • Putting user-controlled instructions in the system prompt.
  • Parsing free text instead of asking for a JSON schema.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Start Here — Build Your First AI Feature.

Practise free

More on Start Here — Build Your First AI Feature