Plan: Use Structured Output (Anthropic OutputConfig) for RCA Service
Summary
The current RCAService relies on the AI provider to format its response as a JSON string within the markdown content, which we then attempt to extract and parse. This is brittle. The plan is to upgrade rca_service.go to use Anthropic's new Structured Output feature (OutputConfigParam), which guarantees that the model will output a JSON object adhering exactly to a provided JSON Schema.
Files to Modify
internal/domain/port/ai_provider.go
- Interface Extension: Add a new method to the
AIProvider interface to support structured outputs explicitly:
SendMessageWithSchema(ctx context.Context, messages []MessageParam, schema ToolInputSchemaParam) (*entity.Message, error)
(Or alternatively, we can update the existing method signatures if preferred, but a new method avoids breaking all existing mocked tests not related to structured output).
internal/infrastructure/adapter/ai/anthropic_adapter.go
- Implementation: Implement
SendMessageWithSchema.
- API Call: Map the domain
schema to anthropic.OutputConfigParam{ Format: anthropic.JSONOutputFormatParam{ Schema: schema, Type: anthropic.JSONSchemaJSONSchema } } (or the equivalent constant in github.com/anthropics/anthropic-sdk-go/shared/constant).
- Pass the
OutputConfig into the anthropic.MessageNewParams.
internal/domain/service/rca_service.go
- Prompt Update: Remove the instruction to "respond with a JSON object" from
rcaPromptTemplate as the schema constraint handles this natively. Keep the schema description minimal.
- Schema Definition: Define a
port.ToolInputSchemaParam representing the expected RCAFinding array structure.
- API Call: Call the new
s.aiProvider.SendMessageWithSchema method.
- Response Handling: Simply use
json.Unmarshal directly on resp.Content. We no longer need to parse out markdown code blocks or handle conversational filler, because the SDK/API guarantees the exact JSON output.
internal/domain/service/rca_service_test.go
- Mock Update: Add the mock implementation for
SendMessageWithSchema to MockAIProvider.
- Test Adjustment: Update
TestRCAService_Correlate to mock SendMessageWithSchema returning the valid JSON response directly in Content.
Implementation Steps
- Add
SendMessageWithSchema to port.AIProvider.
- Implement
SendMessageWithSchema in AnthropicAdapter using OutputConfigParam.
- Update
MockAIProvider and any other mocks (e.g., in other test files) to satisfy the new interface.
- Define the JSON schema in
rca_service.go.
- Update
Correlate implementation to use SendMessageWithSchema and simplify response parsing.
- Update
rca_service_test.go to use the new mock method.
- Run tests
go test ./... to verify correctness and fix any broken mocks.
Considerations
- We must make sure the defined
ToolInputSchemaParam perfectly matches the JSON tags in entity.RCAFinding.
- We will need to verify the exact constant used for
JSONOutputFormatParam.Type in the anthropic-sdk-go package (usually constant.JSONSchema or anthropic.F(...)).
Plan: Use Structured Output (Anthropic OutputConfig) for RCA Service
Summary
The current
RCAServicerelies on the AI provider to format its response as a JSON string within the markdown content, which we then attempt to extract and parse. This is brittle. The plan is to upgraderca_service.goto use Anthropic's new Structured Output feature (OutputConfigParam), which guarantees that the model will output a JSON object adhering exactly to a provided JSON Schema.Files to Modify
internal/domain/port/ai_provider.goAIProviderinterface to support structured outputs explicitly:internal/infrastructure/adapter/ai/anthropic_adapter.goSendMessageWithSchema.schematoanthropic.OutputConfigParam{ Format: anthropic.JSONOutputFormatParam{ Schema: schema, Type: anthropic.JSONSchemaJSONSchema } }(or the equivalent constant ingithub.com/anthropics/anthropic-sdk-go/shared/constant).OutputConfiginto theanthropic.MessageNewParams.internal/domain/service/rca_service.gorcaPromptTemplateas the schema constraint handles this natively. Keep the schema description minimal.port.ToolInputSchemaParamrepresenting the expectedRCAFindingarray structure.s.aiProvider.SendMessageWithSchemamethod.json.Unmarshaldirectly onresp.Content. We no longer need to parse out markdown code blocks or handle conversational filler, because the SDK/API guarantees the exact JSON output.internal/domain/service/rca_service_test.goSendMessageWithSchematoMockAIProvider.TestRCAService_Correlateto mockSendMessageWithSchemareturning the valid JSON response directly inContent.Implementation Steps
SendMessageWithSchematoport.AIProvider.SendMessageWithSchemainAnthropicAdapterusingOutputConfigParam.MockAIProviderand any other mocks (e.g., in other test files) to satisfy the new interface.rca_service.go.Correlateimplementation to useSendMessageWithSchemaand simplify response parsing.rca_service_test.goto use the new mock method.go test ./...to verify correctness and fix any broken mocks.Considerations
ToolInputSchemaParamperfectly matches the JSON tags inentity.RCAFinding.JSONOutputFormatParam.Typein theanthropic-sdk-gopackage (usuallyconstant.JSONSchemaoranthropic.F(...)).