Class AssistantResponseFormatOption
-
- All Implemented Interfaces:
public final class AssistantResponseFormatOptionSpecifies the format that the model must output. Compatible with GPT-4o, [GPT-4
Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models since
gpt-3.5-turbo-1106.
Setting to
{ "type": "json_schema", "json_schema": {...} }enables Structured Outputs which ensures the model will match your supplied JSON schema. Learn more in the Structured Outputs guide.Setting to
{ "type": "json_object" }enables JSON mode, which ensures the message the model generates is valid JSON.Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if
finish_reason="length", which indicates the generation exceededmax_tokensor the conversation exceeded the max context length.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceAssistantResponseFormatOption.VisitorAn interface that defines how to map each variant of AssistantResponseFormatOption to a value of type T.
-
Method Summary
Modifier and Type Method Description final Optional<JsonValue>auto()autois the default valuefinal Optional<ResponseFormatText>responseFormatText()Default response format. final Optional<ResponseFormatJsonObject>responseFormatJsonObject()JSON object response format. final Optional<ResponseFormatJsonSchema>responseFormatJsonSchema()JSON Schema response format. final BooleanisAuto()final BooleanisResponseFormatText()final BooleanisResponseFormatJsonObject()final BooleanisResponseFormatJsonSchema()final JsonValueasAuto()autois the default valuefinal ResponseFormatTextasResponseFormatText()Default response format. final ResponseFormatJsonObjectasResponseFormatJsonObject()JSON object response format. final ResponseFormatJsonSchemaasResponseFormatJsonSchema()JSON Schema response format. final Optional<JsonValue>_json()final <T extends Any> Taccept(AssistantResponseFormatOption.Visitor<T> visitor)Maps this instance's current variant to a value of type T using the given visitor. final AssistantResponseFormatOptionvalidate()Validates that the types of all values in this object match their expected types recursively. final BooleanisValid()Booleanequals(Object other)IntegerhashCode()StringtoString()final static AssistantResponseFormatOptionofAuto()autois the default valuefinal static AssistantResponseFormatOptionofResponseFormatText(ResponseFormatText responseFormatText)Default response format. final static AssistantResponseFormatOptionofResponseFormatJsonObject(ResponseFormatJsonObject responseFormatJsonObject)JSON object response format. final static AssistantResponseFormatOptionofResponseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema)JSON Schema response format. -
-
Method Detail
-
responseFormatText
final Optional<ResponseFormatText> responseFormatText()
Default response format. Used to generate text responses.
-
responseFormatJsonObject
final Optional<ResponseFormatJsonObject> responseFormatJsonObject()
JSON object response format. An older method of generating JSON responses. Using
json_schemais recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.
-
responseFormatJsonSchema
final Optional<ResponseFormatJsonSchema> responseFormatJsonSchema()
JSON Schema response format. Used to generate structured JSON responses. Learn more about Structured Outputs.
-
isResponseFormatText
final Boolean isResponseFormatText()
-
isResponseFormatJsonObject
final Boolean isResponseFormatJsonObject()
-
isResponseFormatJsonSchema
final Boolean isResponseFormatJsonSchema()
-
asResponseFormatText
final ResponseFormatText asResponseFormatText()
Default response format. Used to generate text responses.
-
asResponseFormatJsonObject
final ResponseFormatJsonObject asResponseFormatJsonObject()
JSON object response format. An older method of generating JSON responses. Using
json_schemais recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.
-
asResponseFormatJsonSchema
final ResponseFormatJsonSchema asResponseFormatJsonSchema()
JSON Schema response format. Used to generate structured JSON responses. Learn more about Structured Outputs.
-
accept
final <T extends Any> T accept(AssistantResponseFormatOption.Visitor<T> visitor)
Maps this instance's current variant to a value of type T using the given visitor.
Note that this method is not forwards compatible with new variants from the API, unless visitor overrides Visitor.unknown. To handle variants not known to this version of the SDK gracefully, consider overriding Visitor.unknown:
import com.openai.core.JsonValue; import java.util.Optional; Optional<String> result = assistantResponseFormatOption.accept(new AssistantResponseFormatOption.Visitor<Optional<String>>() { @Override public Optional<String> visitAuto(JsonValue auto) { return Optional.of(auto.toString()); } // ... @Override public Optional<String> unknown(JsonValue json) { // Or inspect the `json`. return Optional.empty(); } });
-
validate
final AssistantResponseFormatOption validate()
Validates that the types of all values in this object match their expected types recursively.
This method is not forwards compatible with new types from the API for existing fields.
-
ofAuto
final static AssistantResponseFormatOption ofAuto()
autois the default value
-
ofResponseFormatText
final static AssistantResponseFormatOption ofResponseFormatText(ResponseFormatText responseFormatText)
Default response format. Used to generate text responses.
-
ofResponseFormatJsonObject
final static AssistantResponseFormatOption ofResponseFormatJsonObject(ResponseFormatJsonObject responseFormatJsonObject)
JSON object response format. An older method of generating JSON responses. Using
json_schemais recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.
-
ofResponseFormatJsonSchema
final static AssistantResponseFormatOption ofResponseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema)
JSON Schema response format. Used to generate structured JSON responses. Learn more about Structured Outputs.
-
-
-
-