Skip to content

Use Anthropic Structured Output for RCA Service #39

Description

@Anthony-Bible

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

  1. Add SendMessageWithSchema to port.AIProvider.
  2. Implement SendMessageWithSchema in AnthropicAdapter using OutputConfigParam.
  3. Update MockAIProvider and any other mocks (e.g., in other test files) to satisfy the new interface.
  4. Define the JSON schema in rca_service.go.
  5. Update Correlate implementation to use SendMessageWithSchema and simplify response parsing.
  6. Update rca_service_test.go to use the new mock method.
  7. 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(...)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions