Java client for the Langfuse API providing access to observability and analytics features for LLM applications
The Langfuse Java client provides a rich set of common types shared across multiple API areas. These types are located in the com.langfuse.client.resources.commons.types package.
import java.time.OffsetDateTime;Basic trace object representing a complete workflow or request.
/**
* Basic trace object
*/
public final class Trace {
String getId();
OffsetDateTime getTimestamp();
Optional<String> getName();
Optional<String> getUserId();
Optional<Object> getMetadata();
Optional<String> getRelease();
Optional<String> getVersion();
Optional<String> getProjectId();
Optional<Boolean> getPublic();
Optional<Boolean> getBookmarked();
Optional<List<String>> getTags();
Optional<Object> getInput();
Optional<Object> getOutput();
Optional<String> getSessionId();
static Builder builder();
}Trace with its observations.
/**
* Trace with observations
*/
public final class TraceWithDetails extends Trace {
List<Observation> getObservations();
static Builder builder();
}Trace with observations and scores (full detail).
/**
* Trace with observations and scores
*/
public final class TraceWithFullDetails extends Trace {
List<Observation> getObservations();
List<ScoreV1> getScores();
static Builder builder();
}Core observation object (span, event, or generation).
/**
* Observation within a trace
* Can be a span, event, or generation
*/
public final class Observation {
String getId();
String getTraceId();
Optional<ObservationType> getType(); // SPAN, EVENT, GENERATION
Optional<String> getParentObservationId();
Optional<String> getName();
Optional<Object> getMetadata();
Optional<String> getModel();
Optional<Object> getModelParameters();
OffsetDateTime getStartTime();
Optional<OffsetDateTime> getEndTime();
Optional<OffsetDateTime> getCompletionStartTime();
Optional<Object> getInput();
Optional<Object> getOutput();
Optional<Usage> getUsage();
Optional<ObservationLevel> getLevel(); // DEBUG, DEFAULT, WARNING, ERROR
Optional<String> getStatusMessage();
Optional<String> getVersion();
static Builder builder();
}Observation with additional computed fields (costs, latency).
/**
* Observation view with computed fields
*/
public final class ObservationsView extends Observation {
Optional<String> getPromptId();
Optional<String> getPromptName();
Optional<Integer> getPromptVersion();
Optional<String> getEnvironment();
Optional<ModelPrice> getModelPrice();
Optional<Double> getTotalCost(); // Computed cost
Optional<Double> getLatency(); // Computed latency in seconds
static Builder builder();
}Session object grouping related traces.
/**
* Session grouping related traces
*/
public final class Session {
String getId();
OffsetDateTime getCreatedAt();
Optional<String> getBookmarked();
Optional<Boolean> getPublic();
List<String> getUserIds();
int getTraceCount();
Optional<Double> getTotalCost();
Optional<String> getEnvironment();
static Builder builder();
}Session with all its traces.
/**
* Session with all traces (not paginated)
*/
public final class SessionWithTraces extends Session {
List<Trace> getTraces();
static Builder builder();
}Union type for different score variants.
/**
* Union type for scores
* Discriminated by value type
*/
public final class Score {
static Score base(BaseScore value);
static Score boolean_(BooleanScore value);
static Score categorical(CategoricalScore value);
static Score numeric(NumericScore value);
<T> T visit(Visitor<T> visitor);
}Base score with generic value.
/**
* Base score type
*/
public final class BaseScore {
String getId();
OffsetDateTime getTimestamp();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
String getProjectId();
String getName();
Object getValue(); // Generic value
ScoreSource getSource(); // API, EVAL, ANNOTATION
Optional<String> getComment();
Optional<String> getTraceId();
Optional<String> getObservationId();
Optional<ScoreDataType> getDataType(); // NUMERIC, CATEGORICAL, BOOLEAN
Optional<String> getConfigId();
Optional<String> getQueueId();
static Builder builder();
}Boolean-valued score.
/**
* Boolean score
*/
public final class BooleanScore extends BaseScore {
boolean getValue();
static Builder builder();
}Categorical score with string value.
/**
* Categorical score
*/
public final class CategoricalScore extends BaseScore {
String getValue();
static Builder builder();
}Numeric score with double value.
/**
* Numeric score
*/
public final class NumericScore extends BaseScore {
double getValue();
static Builder builder();
}Score configuration/template.
/**
* Score configuration template
*/
public final class ScoreConfig {
String getId();
String getName();
ScoreDataType getDataType();
Optional<Boolean> getIsArchived();
Optional<List<ConfigCategory>> getCategories();
Optional<Double> getMinValue();
Optional<Double> getMaxValue();
Optional<String> getDescription();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
static Builder builder();
}Category definition for categorical scores.
/**
* Category for categorical scores
*/
public final class ConfigCategory {
String getValue();
Optional<String> getLabel();
static Builder builder();
}Dataset definition for evaluation.
/**
* Dataset for evaluation
*/
public final class Dataset {
String getId();
String getName();
Optional<String> getDescription();
Optional<Object> getMetadata();
String getProjectId();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
int getItems(); // Number of items
static Builder builder();
}Single item in a dataset (test case).
/**
* Dataset item (test case)
*/
public final class DatasetItem {
String getId();
DatasetStatus getStatus(); // ACTIVE, ARCHIVED
Object getInput();
Optional<Object> getExpectedOutput();
Optional<Object> getMetadata();
Optional<String> getSourceTraceId();
Optional<String> getSourceObservationId();
String getDatasetId();
String getDatasetName();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
static Builder builder();
}Dataset run (evaluation run).
/**
* Dataset run
*/
public final class DatasetRun {
String getId();
String getName();
Optional<String> getDescription();
Optional<Object> getMetadata();
String getDatasetId();
String getDatasetName();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
static Builder builder();
}Dataset run item (links test case to result).
/**
* Dataset run item
*/
public final class DatasetRunItem {
String getId();
String getDatasetRunId();
String getDatasetRunName();
String getDatasetItemId();
Optional<String> getTraceId();
Optional<String> getObservationId();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
static Builder builder();
}Dataset run with all its items.
/**
* Dataset run with items
*/
public final class DatasetRunWithItems extends DatasetRun {
List<DatasetRunItem> getDatasetRunItems();
static Builder builder();
}LLM model definition with pricing.
/**
* LLM model with pricing
*/
public final class Model {
String getId();
String getModelName();
String getMatchPattern(); // Regex for matching
Optional<OffsetDateTime> getStartDate();
Optional<ModelPrice> getInputPrice();
Optional<ModelPrice> getOutputPrice();
Optional<ModelPrice> getTotalPrice();
ModelUsageUnit getUnit(); // TOKENS, CHARACTERS, etc.
Optional<String> getTokenizerId();
Optional<Object> getTokenizerConfig();
static Builder builder();
}Pricing information for a model.
/**
* Model pricing
*/
public final class ModelPrice {
double getPrice();
String getCurrency(); // e.g., "USD"
static Builder builder();
}Token usage information.
/**
* Token usage
*/
public final class Usage {
Optional<Integer> getInput();
Optional<Integer> getOutput();
Optional<Integer> getTotal();
Optional<ModelUsageUnit> getUnit();
Optional<Double> getInputCost();
Optional<Double> getOutputCost();
Optional<Double> getTotalCost();
static Builder builder();
}Comment on an object.
/**
* Comment on a trace, observation, session, or prompt
*/
public final class Comment {
String getId();
String getContent();
CommentObjectType getObjectType(); // TRACE, OBSERVATION, SESSION, PROMPT
String getObjectId();
String getAuthorUserId();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
static Builder builder();
}Union type for map values.
/**
* Union type for map values
* Supports various primitive and complex types
*/
public final class MapValue {
static MapValue string(String value);
static MapValue integer(int value);
static MapValue _double(double value);
static MapValue boolean_(boolean value);
static MapValue list(List<MapValue> value);
static MapValue map(Map<String, MapValue> value);
<T> T visit(Visitor<T> visitor);
}/**
* Type of observation
*/
public enum ObservationType {
SPAN, // Operation or sub-process
EVENT, // Point-in-time occurrence
GENERATION // LLM generation call
}/**
* Log level for observations
*/
public enum ObservationLevel {
DEBUG,
DEFAULT,
WARNING,
ERROR
}/**
* Data type for scores
*/
public enum ScoreDataType {
NUMERIC, // Double value
CATEGORICAL, // String value
BOOLEAN // Boolean value
}/**
* Source of the score
*/
public enum ScoreSource {
ANNOTATION, // Created by annotation
API, // Created via API
EVAL // Created by evaluation
}/**
* Status of dataset item
*/
public enum DatasetStatus {
ACTIVE, // Active, included in evaluations
ARCHIVED // Archived, excluded
}/**
* Unit for usage measurement
*/
public enum ModelUsageUnit {
CHARACTERS,
TOKENS,
REQUESTS,
IMAGES,
SECONDS
}/**
* Object types that can have comments
*/
public enum CommentObjectType {
TRACE,
OBSERVATION,
SESSION,
PROMPT
}Interface for trace properties.
/**
* Interface for trace properties
*/
public interface ITrace {
String getId();
OffsetDateTime getTimestamp();
Optional<String> getName();
Optional<String> getUserId();
Optional<Object> getMetadata();
// ... other trace properties
}Interface for observation properties.
/**
* Interface for observation properties
*/
public interface IObservation {
String getId();
String getTraceId();
Optional<ObservationType> getType();
Optional<String> getName();
OffsetDateTime getStartTime();
Optional<OffsetDateTime> getEndTime();
// ... other observation properties
}Interface for session properties.
/**
* Interface for session properties
*/
public interface ISession {
String getId();
OffsetDateTime getCreatedAt();
List<String> getUserIds();
// ... other session properties
}Interface for base score properties.
/**
* Interface for score properties
*/
public interface IBaseScore {
String getId();
OffsetDateTime getTimestamp();
OffsetDateTime getCreatedAt();
OffsetDateTime getUpdatedAt();
String getName();
Object getValue();
ScoreSource getSource();
// ... other score properties
}Interface for dataset run properties.
/**
* Interface for dataset run properties
*/
public interface IDatasetRun {
String getId();
String getName();
Optional<String> getDescription();
Optional<Object> getMetadata();
String getDatasetId();
String getDatasetName();
// ... other dataset run properties
}// Score is a union type - use visitor pattern
Score score = client.scoreV2().getById("score-123");
score.visit(new Score.Visitor<Void>() {
@Override
public Void visitNumeric(NumericScore numeric) {
System.out.println("Numeric: " + numeric.getValue());
return null;
}
@Override
public Void visitCategorical(CategoricalScore categorical) {
System.out.println("Categorical: " + categorical.getValue());
return null;
}
@Override
public Void visitBoolean(BooleanScore bool) {
System.out.println("Boolean: " + bool.getValue());
return null;
}
@Override
public Void visitBase(BaseScore base) {
System.out.println("Base: " + base.getValue());
return null;
}
@Override
public Void visitUnknown(String unknownType) {
return null;
}
});Observation observation = ...;
// Check observation type
if (observation.getType().isPresent()) {
switch (observation.getType().get()) {
case SPAN:
System.out.println("This is a span");
break;
case EVENT:
System.out.println("This is an event");
break;
case GENERATION:
System.out.println("This is a generation");
if (observation.getModel().isPresent()) {
System.out.println("Model: " + observation.getModel().get());
}
break;
}
}
// Check observation level
if (observation.getLevel().isPresent() &&
observation.getLevel().get() == ObservationLevel.ERROR) {
System.err.println("Error in observation: " +
observation.getStatusMessage().orElse("no message"));
}ObservationsView view = client.observations().get("obs-123");
if (view.getUsage().isPresent()) {
Usage usage = view.getUsage().get();
System.out.println("Input tokens: " + usage.getInput().orElse(0));
System.out.println("Output tokens: " + usage.getOutput().orElse(0));
System.out.println("Total tokens: " + usage.getTotal().orElse(0));
if (usage.getTotalCost().isPresent()) {
System.out.println("Cost: $" + usage.getTotalCost().get());
}
}Trace
├── TraceWithDetails (adds observations)
└── TraceWithFullDetails (adds observations + scores)
Observation
└── ObservationsView (adds computed fields)
Session
└── SessionWithTraces (adds traces)
DatasetRun
└── DatasetRunWithItems (adds items)
Score (union)
├── BaseScore
├── BooleanScore
├── CategoricalScore
└── NumericScoreInstall with Tessl CLI
npx tessl i tessl/maven-com-langfuse--langfuse-java