Class StructuredOutputSchemas

java.lang.Object
io.micronaut.langchain4j.jsonschema.StructuredOutputSchemas

@Singleton @Internal @Requires(property="langchain4j.structured-output.enabled",notEquals="false",defaultValue="true") @Requires(beans=StructuredOutputSchemaProvider.class) public final class StructuredOutputSchemas extends Object
Configures the AI services and agents whose methods return a type with a schema supplied by a StructuredOutputSchemaProvider (by default, a type annotated with io.micronaut.jsonschema.JsonSchema), so that this schema is sent to the chat model as the structured output format.

Methods returning T, List<T> or Set<T>, optionally wrapped in Result, CompletableFuture or CompletionStage, are supported. LangChain4j still decides when a response format is sent and parses the response; only the schema is replaced.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    configure(@NonNull dev.langchain4j.service.AiServiceContext context, @NonNull Iterable<? extends io.micronaut.core.type.Argument<?>> returnTypes)
    Configures an AI service: its chat requests carry the provided schemas, and its chat model declares Capability.RESPONSE_FORMAT_JSON_SCHEMA if needed, see declaresJsonSchemaCapability(ChatModel).
    boolean
    declaresJsonSchemaCapability(@NonNull dev.langchain4j.model.chat.ChatModel chatModel)
    Whether LangChain4j sends a JSON schema response format to the given model, possibly once declared.
    @NonNull dev.langchain4j.model.chat.ChatModel
    decorate(@NonNull dev.langchain4j.model.chat.ChatModel chatModel, @NonNull ResponseFormatSchemas schemas, @NonNull Class<?> agentType)
    Decorates the chat model of an agent, for which LangChain4j offers no request transformer, so that its chat requests carry the provided schemas.
    schemasFor(@NonNull Iterable<? extends io.micronaut.core.type.Argument<?>> returnTypes)
    Resolves the schemas of the structured outputs returned by the methods of a service.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • schemasFor

      public @NonNull Optional<ResponseFormatSchemas> schemasFor(@NonNull Iterable<? extends io.micronaut.core.type.Argument<?>> returnTypes)
      Resolves the schemas of the structured outputs returned by the methods of a service.
      Parameters:
      returnTypes - The generic return types of the methods
      Returns:
      The schemas, or empty if no return type has a provided schema
    • configure

      public void configure(@NonNull dev.langchain4j.service.AiServiceContext context, @NonNull Iterable<? extends io.micronaut.core.type.Argument<?>> returnTypes)
      Configures an AI service: its chat requests carry the provided schemas, and its chat model declares Capability.RESPONSE_FORMAT_JSON_SCHEMA if needed, see declaresJsonSchemaCapability(ChatModel).
      Parameters:
      context - The context of the AI service, with its chat model configured
      returnTypes - The generic return types of the methods of the service
    • decorate

      public @NonNull dev.langchain4j.model.chat.ChatModel decorate(@NonNull dev.langchain4j.model.chat.ChatModel chatModel, @NonNull ResponseFormatSchemas schemas, @NonNull Class<?> agentType)
      Decorates the chat model of an agent, for which LangChain4j offers no request transformer, so that its chat requests carry the provided schemas.
      Parameters:
      chatModel - The chat model of the agent
      schemas - The schemas, see schemasFor(Iterable)
      agentType - The agent type, for logging
      Returns:
      The decorated chat model
    • declaresJsonSchemaCapability

      public boolean declaresJsonSchemaCapability(@NonNull dev.langchain4j.model.chat.ChatModel chatModel)
      Whether LangChain4j sends a JSON schema response format to the given model, possibly once declared.
      Parameters:
      chatModel - The chat model
      Returns:
      true if the model declares the capability, or is forced to, see StructuredOutputConfiguration