@@ -134,7 +134,7 @@ def test_mistral():
134134 from langchain_mistralai .chat_models import ChatMistralAI
135135
136136 api = get_api ()
137- callback = CallbackHandler (debug = True )
137+ callback = CallbackHandler (debug = False )
138138
139139 chat = ChatMistralAI (model = "mistral-small" , callbacks = [callback ])
140140 messages = [HumanMessage (content = "say a brief hello" )]
@@ -242,7 +242,41 @@ def test_basic_chat_openai():
242242 trace = api .trace .get (trace_id )
243243
244244 assert trace .id == trace_id
245- assert len (trace .observations ) == 2
245+ assert len (trace .observations ) == 1
246+
247+
248+ def test_basic_chat_openai_based_on_trace ():
249+ from langchain .schema import HumanMessage , SystemMessage
250+
251+ trace_id = create_uuid ()
252+
253+ langfuse = Langfuse (debug = False )
254+ trace = langfuse .trace (id = trace_id )
255+
256+ callback = trace .get_langchain_handler ()
257+
258+ chat = ChatOpenAI (temperature = 0 )
259+
260+ messages = [
261+ SystemMessage (
262+ content = "You are a helpful assistant that translates English to French."
263+ ),
264+ HumanMessage (
265+ content = "Translate this sentence from English to French. I love programming."
266+ ),
267+ ]
268+
269+ chat (messages , callbacks = [callback ])
270+ callback .flush ()
271+
272+ trace_id = callback .get_trace_id ()
273+
274+ api = get_api ()
275+
276+ trace = api .trace .get (trace_id )
277+
278+ assert trace .id == trace_id
279+ assert len (trace .observations ) == 1
246280
247281
248282def test_callback_from_trace_simple_chain ():
@@ -630,7 +664,7 @@ def test_callback_simple_openai():
630664
631665 trace = api .trace .get (trace_id )
632666
633- assert len (trace .observations ) == 2
667+ assert len (trace .observations ) == 1
634668 assert trace .input == trace .observations [0 ].input
635669 for observation in trace .observations :
636670 if observation .type == "GENERATION" :
@@ -670,7 +704,7 @@ def test_callback_multiple_invocations_on_different_traces():
670704 {"trace" : trace_one , "expected_trace_id" : trace_id_one },
671705 {"trace" : trace_two , "expected_trace_id" : trace_id_two },
672706 ]:
673- assert len (test_data ["trace" ].observations ) == 2
707+ assert len (test_data ["trace" ].observations ) == 1
674708 assert test_data ["trace" ].id == test_data ["expected_trace_id" ]
675709 for observation in test_data ["trace" ].observations :
676710 if observation .type == "GENERATION" :
@@ -688,7 +722,7 @@ def test_callback_simple_openai_streaming():
688722 api_wrapper = LangfuseAPI ()
689723 handler = CallbackHandler (debug = False )
690724
691- llm = OpenAI (openai_api_key = os .environ .get ("OPENAI_API_KEY" ), streaming = True )
725+ llm = OpenAI (openai_api_key = os .environ .get ("OPENAI_API_KEY" ), streaming = False )
692726
693727 text = "What would be a good company name for a company that makes laptops?"
694728
0 commit comments