22 auto schema = ParamSchema<T>::schema();
24 std::string last_finish_reason;
25 std::string last_error;
27 for (
int attempt = 0; attempt <= config.max_retries; ++attempt) {
29 request.output_schema = schema;
33 response = provider.
chat(working, request);
34 }
catch (
const std::exception& e) {
36 accumulated, attempt + 1,
"error");
39 accumulated.input_tokens += response.usage.input_tokens;
40 accumulated.output_tokens += response.usage.output_tokens;
41 last_finish_reason = response.finish_reason;
42 working.
add(response.message);
46 AgentResult<T> result;
47 result.output = json.template get<T>();
48 result.conversation = std::move(working);
49 result.total_usage = accumulated;
50 result.iterations = attempt + 1;
51 result.finish_reason = std::move(last_finish_reason);
53 }
catch (
const std::exception& e) {
54 last_error = e.what();
55 if (attempt < config.max_retries) {
57 Role::user,
"Your response did not match the required schema: " + last_error +
58 ". Please respond with valid JSON matching the schema."));
64 "structured output failed after " + std::to_string(config.max_retries + 1) +
65 " attempts: " + last_error,
66 std::move(working), accumulated, config.max_retries + 1,
67 std::move(last_finish_reason));
AgentResult< T > structured(LlmProvider &provider, Conversation working, StructuredConfig config={})
Definition structured.hpp:20