I am experimenting little bit with Agent and langchain4j.
I have one Agent with the following snippet in System Prompt.
Output format contract (MANDATORY):
You must return a single valid JSON object and nothing else.
Do not include markdown, prose, headings, explanations, or formatting.
Do not wrap the JSON in code fences.
The JSON structure must be exactly:
{
"offers": [
{
"hotelId": string,
"hotelName": string,
"roomDescription": string,
"totalPrice": string,
"currency": string
}
]
}
Rules:
- The root object must contain only the "offers" key.
- "offers" must be an array.
- Each array element represents exactly one room offer.
- All fields are mandatory.
- Values must be copied verbatim from tool responses.
- Do not add, remove, rename, or nest fields.
- Do not include null values.
- If no offers are available, return:
{ "offers": [] }
Any response that is not valid JSON matching this schema is invalid.
with that Agent returns a valid JSON as I expected.
But I know that langchain4j have an Structured Output functionality so I implemented the following
@AiService
public interface HotelOffersExtractor {
HotelOffersResponse extractOffersFrom(String text);
}
HotelOffersExtractor hotelOffersExtractor = AiServices.create(HotelOffersExtractor.class, chatModel);
HotelOffersResponse response = hotelOffersExtractor.extractOffersFrom(result.content());
but by looking in depth, I saw that this cause another call to the Model API, isn't this a bad idea, while in production environment this will cause me more money I think.
So if my first solution in System Prompt works, does it make sense to use Structured Output feature of langchain4j.
Thx for answers.