0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LangfuseのスコアはPOSTメソッドで作成と更新をするらしい

0
Last updated at Posted at 2025-12-31

LangfuseのスコアをAPI経由で更新する方法を紹介します。少しクセのある仕様なので、その点も含めて解説します。Langfuseのスコア機能そのものについては、以前にブログで紹介しているため、詳細はそちらを参照してください。

LangfuseのAPI体系とスコアの更新方法

LangfuseのAPIには、スコアに関連する以下のエンドポイントがあります。

  • /api/public/v2/scores GET
  • /api/public/v2/scores/{scoreId} GET
  • /api/public/scores POST
  • /api/public/scores/{scoreId} DEL

GETは参照系のAPI、DELは削除系のAPIです。少しややこしいのがPOSTのAPIで、このエンドポイントは作成と更新の両方を兼ねた仕様になっています。

具体的には、APIリクエストのパラメータにscore_idが含まれている場合は更新処理、score_idが含まれていない場合は新規作成処理として扱われます。

たとえば、以下のコードはスコアの新規作成になります。

from langfuse import get_client

langfuse = get_client()
trace = langfuse.api.trace.get(trace_id="93fa7a107ed90d011c9e51a3b8ce8d22")
print(f"Trace Name: {trace.name}, ID: {trace.id}")

scores=langfuse.api.score_v_2.get(trace_id=trace.id)
for score in scores.data:
    print(f"Score Name: {score.name}, Value: {score.value}, ID: {score.id}")

langfuse.create_score(
    trace_id=trace.id,
    name="Sample",
    value="var",
)

一方、以下のようにcreate_scoreメソッドの引数にscore_idを指定すると、そのIDに対応するスコアが更新されます。違いはscore_idを指定しているかどうかだけです。

from langfuse import get_client

langfuse = get_client()
trace = langfuse.api.trace.get(trace_id="93fa7a107ed90d011c9e51a3b8ce8d22")
print(f"Trace Name: {trace.name}, ID: {trace.id}")

scores=langfuse.api.score_v_2.get(trace_id=trace.id)
for score in scores.data:
    print(f"Score Name: {score.name}, Value: {score.value}, ID: {score.id}")

langfuse.create_score(
    trace_id=trace.id,
    score_id="e3575a6a2a3c5db9", # <- スコアのIDを指定する
    name="Sample",
    value="var",
)

REST APIに慣れていると、POSTメソッドには「作成」のイメージを持つことが多いため、更新まで担っている点に少し違和感を覚えるかもしれません。

ちなみに、

作成処理を複数回実行するとどうなるか

作成処理を実行した回数分だけスコアが作成されます。以下の画像は、作成処理を3回実行した例で、Sampleスコアにvar値が3つ付与されています。

CleanShot 2025-12-31 at 13.58.48.png

公式ドキュメント曰く、複数の値を設定可能にすることで、時系列による変化を追跡したい場合や複数のユーザーからフィードバックを得たい際に便利みたいです。

GitHubのDiscussionでも議論されています

スコアの更新方法についてはGitHubのDiscussionでも質問されており、Langfuse側からはscore_idを指定して更新する、という回答がされています。

LangFuse is doing upsert operation based on if the score id is provided or not. If provided, it will try to perform update, otherwise it will create a new score.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?