Anthropic | Structured Outputs

Structured Outputs는 Claude가 JSON 스키마를 준수하는 응답을 반환하도록 강제합니다. 이는 스키마 정의와 일치하는 유효하고 파싱 가능한 JSON 출력을 보장합니다.

간단한 예제

JSON 스키마 출력으로 메시지를 만들려면 편의 메서드를 사용하십시오.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';

vSchema := '{"type":"object","properties":{"name":{"type":"string"},' +
  '"age":{"type":"integer"}},"required":["name","age"],' +
  '"additionalProperties":false}';

WriteLn(Anthropic._CreateMessageJSON('claude-sonnet-4-20250514',
  'Extract the name and age: John is 30 years old.', vSchema));

고급 예제

구조화된 출력을 effort 매개변수와 결합하려면 형식화된 클래스를 사용하십시오.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';

oRequest := TsgcAnthropicClass_Request_Messages.Create;
Try
  oRequest.Model := 'claude-sonnet-4-20250514';
  oRequest.MaxTokens := 4096;

  // JSON schema for structured output
  oRequest.OutputFormatSchema :=
    '{"type":"object","properties":{"sentiment":{"type":"string",' +
    '"enum":["positive","negative","neutral"]},"confidence":' +
    '{"type":"number"}},"required":["sentiment","confidence"],' +
    '"additionalProperties":false}';

  // Set effort level (low, medium, high, max)
  oRequest.Effort := 'medium';

  oMessage := TsgcAnthropicClass_Request_Message.Create;
  oMessage.Role := 'user';
  oMessage.Content := 'Analyze the sentiment: I love this product!';
  oMessages := oRequest.Messages;
  SetLength(oMessages, 1);
  oMessages[0] := oMessage;
  oRequest.Messages := oMessages;

  oResponse := Anthropic.CreateMessage(oRequest);
  Try
    for i := 0 to Length(oResponse.Content) - 1 do
    begin
      if oResponse.Content[i].ContentType = 'text' then
        WriteLn(oResponse.Content[i].Text);
    end;
  Finally
    oResponse.Free;
  End;
Finally
  oMessage.Free;
  oRequest.Free;
End;

Strict Tool Use

도구 입력이 input_schema와 정확히 일치하도록 보장하기 위해 도구 정의에서 strict 모드를 활성화합니다.


oTool := TsgcAnthropicClass_Request_Tool.Create;
oTool.Name := 'get_weather';
oTool.Description := 'Get the current weather for a location';
oTool.Strict := True;  // Guarantee schema conformance
oTool.InputSchema := '{"type":"object","properties":{"location":' +
  '{"type":"string"}},"required":["location"],"additionalProperties":false}';

Properties