Add JSON-schema-constrained chat completion (json_schema / response_format) #59
Labels
No labels
prio_critical
prio_low
type_bug
type_contact
type_issue
type_lead
type_question
type_story
type_task
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
lhumina_code/hero_aibroker#59
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Need: JSON-schema-constrained chat completion
Per hero_memory ADR-0006,
hero_memoryroutes all LLM calls throughhero_aibroker. The Q&A extractor (Phase 4) and the ontology extractor (Phase 5) both require provider-agnostic JSON-schema-constrained output — the model returns JSON guaranteed to match a given schema.This is missing today. Audit summary:
ai.chatJSON-RPC method (openrpc.json:579–618): nojson_schemaparameterPOST /v1/chat/completionsREST endpoint (crates/hero_aibroker_server/src/api/chat.rs:17–42): noresponse_format/json_schemafieldChatRequest(crates/hero_aibroker_lib/src/providers/types.rs:120–145): no schema fieldOpenAIProviderdoes not forward any structured-output constraintProposal
Add
json_schema: Option<serde_json::Value>(orresponse_format: Option<ResponseFormat>matching OpenAI's shape) toChatRequest. Pass it through the OpenAI-compatible providers (OpenAI, OpenRouter, Groq, SambaNova) which all acceptresponse_format = { "type": "json_schema", "json_schema": {...} }. Surface in:ai.chatRPC method (new optional param)POST /v1/chat/completionsREST request struct (new optional field)The audit estimated this as a small change (< 1 day), no breaking changes since all fields are optional.
What hero_memory needs from the broker, exact shape
timeout_msis also currently fixed at 300s in the OpenAI client builder — would be useful per-request but lower priority thanjson_schema.Why this is blocking
{"pairs": [{"question": "...", "answer": "...", "anchor": "..."}]}— without schema constraint, free-form responses break parsing.Acceptance
ChatRequestacceptsjson_schema(or equivalent structured-output field).response_format.ai.chatRPC accepts the new field.POST /v1/chat/completionsaccepts the new field.References hero_memory issue: lhumina_code/hero_memory#1
Branch + PR up: #60 (commit
ebfd358).Small change as the audit predicted —
response_format: Option<Value>onChatRequestandOpenAIChatRequest, forwarded unchanged to upstream.ai.chatRPC documents the new optional param. Pass-through only — callers (hero_memory) build the schema.Resolved upstream.
response_formatis already onChatRequest(typedResponseFormat { kind, json_schema }) and exposed viaai.chatin openrpc.json ondevelopment, courtesy of the cascade multi-broker change. hero_memory's Q&A and ontology extractors are now pinned todevelopmentdirectly. PR #60 closed as redundant.