Annotation Type PromptConfigJsonDiscriminator


  • @Documented
    @Target(FIELD)
    @Retention(RUNTIME)
    public @interface PromptConfigJsonDiscriminator
    Annotation for the discriminator field of a model class that represents the abstract base class of a discriminated union, intended for use with the JsonConfig mixin for prompt service handlers.

    A discriminated union is a set of classes that share a common base class and are distinguished by a discriminator property. The discriminator property is a field in the base class that contains a value that indicates which subclass should be instantiated when deserializing a JSON object.

    To properly serialize and deserialize discriminated unions, you must place the following 3 annotations:

    For example:
     @PromptConfigJsonDiscriminatedUnion({BananaSplit.class})
     public class Sweets {
       // other fields needed by this class
       @PromptConfigJsonDiscriminator
       protected String type;
     }
    
     @PromptConfigJsonDiscriminatedUnionMember("banana_split")
     public class BananaSplit extends Sweets {
       // other fields needed by this class
       public BananaSplit() {
           this.type = "banana_split";
       }
     }
    
     @PromptConfigJsonDiscriminatedUnionMember("apple_pie")
     public class ApplePie extends Sweets {
       // other fields needed by this class
       public ApplePie() {
        this.type = "apple_pie";
       }
     }
     
    In this example, we'd expect the JSON object to contain the "type" field. If the field's value is "banana_split", then we should deserialize the object into an instance of the BananaSplit class, and if the field's value is "apple_pie", then we should deserialize the object into an instance of the ApplePie class.
    Since:
    8.5.0