Alex
Alex13mo ago

Hi y all I m having trouble logging

Hi y'all, I'm having trouble logging score with the python sdk. I have a minimal example I'll put in a thread here
4 Replies
Alex
Alex13mo ago
from langfuse import Langfuse
from langfuse.model import CreateTrace, CreateGeneration, CreateScore


langfuse = Langfuse(
public_key="...",
secret_key="...",
host="http://localhost:3000",
)


trace = langfuse.trace(CreateTrace())
generation = trace.generation(CreateGeneration(
name="testgeneration",
prompt=[{"role": "user", "content": "hello world"}],
completion="hi there",
))

print(f"{generation.id=}")

score = langfuse.score(CreateScore(name='testscore', value=0.5, observationId=generation.id))
from langfuse import Langfuse
from langfuse.model import CreateTrace, CreateGeneration, CreateScore


langfuse = Langfuse(
public_key="...",
secret_key="...",
host="http://localhost:3000",
)


trace = langfuse.trace(CreateTrace())
generation = trace.generation(CreateGeneration(
name="testgeneration",
prompt=[{"role": "user", "content": "hello world"}],
completion="hi there",
))

print(f"{generation.id=}")

score = langfuse.score(CreateScore(name='testscore', value=0.5, observationId=generation.id))
running this I get
generation.id='129ddebb-49a9-404d-8878-6b8467a381b4'
'CreateScore' object has no attribute 'observation_id'
Traceback (most recent call last):
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/client.py", line 108, in score
if body.observation_id is not None:
AttributeError: 'CreateScore' object has no attribute 'observation_id'
1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
Traceback (most recent call last):
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/client.py", line 101, in task
return self.client.score.create(request=new_body)
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/api/resources/score/client.py", line 55, in create
raise Error(pydantic.parse_obj_as(str, _response.json())) # type: ignore
File "pydantic/tools.py", line 38, in pydantic.tools.parse_obj_as
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
Task ecc00a13-ecb2-46e5-83e7-5045e03c44e1 failed with exception 1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
generation.id='129ddebb-49a9-404d-8878-6b8467a381b4'
'CreateScore' object has no attribute 'observation_id'
Traceback (most recent call last):
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/client.py", line 108, in score
if body.observation_id is not None:
AttributeError: 'CreateScore' object has no attribute 'observation_id'
1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
Traceback (most recent call last):
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/client.py", line 101, in task
return self.client.score.create(request=new_body)
File "/home/alex/repos/zenai/env/lib/python3.10/site-packages/langfuse/api/resources/score/client.py", line 55, in create
raise Error(pydantic.parse_obj_as(str, _response.json())) # type: ignore
File "pydantic/tools.py", line 38, in pydantic.tools.parse_obj_as
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
Task ecc00a13-ecb2-46e5-83e7-5045e03c44e1 failed with exception 1 validation error for ParsingModel[str]
__root__
str type expected (type=type_error.str)
Max
Max13mo ago
Can you try this one here?
from langfuse.model import InitialScore

langfuse.score(InitialScore(
trace_id=trace.id,
observation_id=generation.id,
name="quality",
value=1,
comment="Factually correct",
))
from langfuse.model import InitialScore

langfuse.score(InitialScore(
trace_id=trace.id,
observation_id=generation.id,
name="quality",
value=1,
comment="Factually correct",
))
Alex
Alex13mo ago
This seems to work as expected switching CreateScore for InitialScore, thanks!
Max
Max13mo ago
Yes, thanks a lot!