Class Metadata

java.lang.Object
dev.langchain4j.data.document.Metadata

public class Metadata extends Object
Represents metadata of a Document or a TextSegment.
For a Document, the metadata could store information such as the source, creation date, owner, or any other relevant details.
For a TextSegment, in addition to metadata inherited from a Document, it can also include segment-specific information, such as the page number, the position of the segment within the document, chapter, etc.
The metadata is stored as a key-value map, where the key is a String and the value can be one of: String, UUID, Integer, Long, Float, Double. If you require additional types, please open an issue.
null values are not permitted.
  • Constructor Details Link icon

    • Metadata Link icon

      public Metadata()
      Construct a Metadata object with an empty map of key-value pairs.
    • Metadata Link icon

      public Metadata(Map<String,?> metadata)
      Constructs a Metadata object from a map of key-value pairs.
      Parameters:
      metadata - the map of key-value pairs; must not be null. null values are not permitted. Supported value types: String, Integer, Long, Float, Double
  • Method Details Link icon

    • get Link icon

      @Deprecated(forRemoval=true) public @Nullable String get(String key)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Returns the value associated with the given key.
      Parameters:
      key - the key
      Returns:
      the value associated with the given key, or null if the key is not present.
    • getString Link icon

      public @Nullable String getString(String key)
      Returns the String value associated with the given key.
      Parameters:
      key - the key
      Returns:
      the String value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not of type String
    • getUUID Link icon

      public @Nullable UUID getUUID(String key)
      Returns the UUID value associated with the given key.
      Parameters:
      key - the key
      Returns:
      the UUID value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not of type String
    • getInteger Link icon

      public @Nullable Integer getInteger(String key)
      Returns the Integer value associated with the given key.
      Some EmbeddingStore implementations (still) store Metadata values as Strings. In this case, the String value will be parsed into an Integer when this method is called.
      Some EmbeddingStore implementations store Metadata key-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize an Integer and then deserialize it as a Long. Or serialize a Float and then deserialize it as a Double, and so on. In such cases, the actual value will be cast to an Integer when this method is called.
      Parameters:
      key - the key
      Returns:
      the Integer value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not Number
    • getLong Link icon

      public @Nullable Long getLong(String key)
      Returns the Long value associated with the given key.
      Some EmbeddingStore implementations (still) store Metadata values as Strings. In this case, the String value will be parsed into an Long when this method is called.
      Some EmbeddingStore implementations store Metadata key-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize an Integer and then deserialize it as a Long. Or serialize a Float and then deserialize it as a Double, and so on. In such cases, the actual value will be cast to a Long when this method is called.
      Parameters:
      key - the key
      Returns:
      the Long value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not Number
    • getFloat Link icon

      public @Nullable Float getFloat(String key)
      Returns the Float value associated with the given key.
      Some EmbeddingStore implementations (still) store Metadata values as Strings. In this case, the String value will be parsed into a Float when this method is called.
      Some EmbeddingStore implementations store Metadata key-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize an Integer and then deserialize it as a Long. Or serialize a Float and then deserialize it as a Double, and so on. In such cases, the actual value will be cast to a Float when this method is called.
      Parameters:
      key - the key
      Returns:
      the Float value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not Number
    • getDouble Link icon

      public @Nullable Double getDouble(String key)
      Returns the Double value associated with the given key.
      Some EmbeddingStore implementations (still) store Metadata values as Strings. In this case, the String value will be parsed into a Double when this method is called.
      Some EmbeddingStore implementations store Metadata key-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize an Integer and then deserialize it as a Long. Or serialize a Float and then deserialize it as a Double, and so on. In such cases, the actual value will be cast to a Double when this method is called.
      Parameters:
      key - the key
      Returns:
      the Double value associated with the given key, or null if the key is not present.
      Throws:
      RuntimeException - if the value is not Number
    • containsKey Link icon

      public boolean containsKey(String key)
      Check whether this Metadata contains a given key.
      Parameters:
      key - the key
      Returns:
      true if this metadata contains a given key; false otherwise.
    • add Link icon

      @Deprecated(forRemoval=true) public Metadata add(String key, Object value)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • add Link icon

      @Deprecated(forRemoval=true) public Metadata add(String key, String value)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, String value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, UUID value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, int value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, long value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, float value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • put Link icon

      public Metadata put(String key, double value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • remove Link icon

      public Metadata remove(String key)
      Removes the given key from the metadata.
      Parameters:
      key - the key
      Returns:
      this
    • copy Link icon

      public Metadata copy()
      Copies the metadata.
      Returns:
      a copy of this Metadata object.
    • asMap Link icon

      @Deprecated(forRemoval=true) public Map<String,String> asMap()
      Deprecated, for removal: This API element is subject to removal in a future version.
      as of 0.31.0, use toMap() instead.
      Get a copy of the metadata as a map of key-value pairs.
      Returns:
      the metadata as a map of key-value pairs.
    • toMap Link icon

      public Map<String,Object> toMap()
      Get a copy of the metadata as a map of key-value pairs.
      Returns:
      the metadata as a map of key-value pairs.
    • equals Link icon

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode Link icon

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • from Link icon

      public static Metadata from(String key, String value)
      Constructs a Metadata object from a single key-value pair.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • from Link icon

      @Deprecated(forRemoval=true) public static Metadata from(String key, Object value)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • from Link icon

      public static Metadata from(Map<String,?> metadata)
      Constructs a Metadata object from a map of key-value pairs.
      Parameters:
      metadata - the map of key-value pairs
      Returns:
      a Metadata object
    • metadata Link icon

      public static Metadata metadata(String key, String value)
      Constructs a Metadata object from a single key-value pair.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • metadata Link icon

      @Deprecated(forRemoval=true) public static Metadata metadata(String key, Object value)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • merge Link icon

      public Metadata merge(@Nullable Metadata another)
      Merges the current Metadata object with another Metadata object. The two Metadata objects must not have any common keys.
      Parameters:
      another - The Metadata object to be merged with the current Metadata object.
      Returns:
      A new Metadata object that contains all key-value pairs from both Metadata objects.
      Throws:
      IllegalArgumentException - if there are common keys between the two Metadata objects.