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

    • Metadata

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

      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

    • get

      @Deprecated(forRemoval=true) public 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

      public 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

      public 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

      public 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

      public 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

      public 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

      public 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

      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

      @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

      @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

      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

      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

      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

      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

      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

      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

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

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

      @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

      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

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

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

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

      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

      @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

      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

      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

      @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