Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 587dc96

Browse files
authored
Merge pull request #168 from Chainlit/willy/lc-reasoning
feat: add reasoning token support to lc
2 parents 0efad4e + 561240c commit 587dc96

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

literalai/callback/langchain_callback.py

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,33 @@ def _convert_message_dict(
9292
if function_call:
9393
msg["function_call"] = function_call
9494
else:
95-
msg["content"] = kwargs.get("content", "")
95+
content = kwargs.get("content")
96+
if isinstance(content, list):
97+
tool_calls = []
98+
content_parts = []
99+
for item in content:
100+
if item.get("type") == "tool_use":
101+
tool_calls.append(
102+
{
103+
"id": item.get("id"),
104+
"type": "function",
105+
"function": {
106+
"name": item.get("name"),
107+
"arguments": item.get("input"),
108+
},
109+
}
110+
)
111+
elif item.get("type") == "text":
112+
content_parts.append(
113+
{"type": "text", "text": item.get("text")}
114+
)
115+
116+
if tool_calls:
117+
msg["tool_calls"] = tool_calls
118+
if content_parts:
119+
msg["content"] = content_parts # type: ignore
120+
else:
121+
msg["content"] = content # type: ignore
96122

97123
if tool_calls:
98124
msg["tool_calls"] = tool_calls
@@ -123,7 +149,34 @@ def _convert_message(
123149
if function_call:
124150
msg["function_call"] = function_call
125151
else:
126-
msg["content"] = message.content # type: ignore
152+
if isinstance(message.content, list):
153+
tool_calls = []
154+
content_parts = []
155+
for item in message.content:
156+
if isinstance(item, str):
157+
continue
158+
if item.get("type") == "tool_use":
159+
tool_calls.append(
160+
{
161+
"id": item.get("id"),
162+
"type": "function",
163+
"function": {
164+
"name": item.get("name"),
165+
"arguments": item.get("input"),
166+
},
167+
}
168+
)
169+
elif item.get("type") == "text":
170+
content_parts.append(
171+
{"type": "text", "text": item.get("text")}
172+
)
173+
174+
if tool_calls:
175+
msg["tool_calls"] = tool_calls
176+
if content_parts:
177+
msg["content"] = content_parts # type: ignore
178+
else:
179+
msg["content"] = message.content # type: ignore
127180

128181
if tool_calls:
129182
msg["tool_calls"] = tool_calls
@@ -201,7 +254,12 @@ def _build_llm_settings(
201254
{"type": "function", "function": f} for f in settings["functions"]
202255
]
203256
if "tools" in settings:
204-
tools = settings["tools"]
257+
tools = [
258+
{"type": "function", "function": t}
259+
if t.get("type") != "function"
260+
else t
261+
for t in settings["tools"]
262+
]
205263
return provider, model, tools, settings
206264

207265
DEFAULT_TO_IGNORE = [
@@ -411,7 +469,9 @@ def _start_trace(self, run: Run) -> None:
411469
)
412470
step.tags = run.tags
413471
step.metadata = run.metadata
414-
step.input = self.process_content(run.inputs)
472+
473+
if step.type != "llm":
474+
step.input = self.process_content(run.inputs)
415475

416476
self.steps[str(run.id)] = step
417477

@@ -484,7 +544,6 @@ def _on_run_update(self, run: Run) -> None:
484544
if v is not None
485545
}
486546

487-
current_step.output = message_completion
488547
else:
489548
completion_start = self.completion_generations[str(run.id)]
490549
duration = time.time() - completion_start["start"]
@@ -509,7 +568,6 @@ def _on_run_update(self, run: Run) -> None:
509568
output_token_count=usage_metadata.get("output_tokens"),
510569
token_count=usage_metadata.get("total_tokens"),
511570
)
512-
current_step.output = {"content": completion}
513571

514572
if current_step:
515573
if current_step.metadata is None:
@@ -521,7 +579,8 @@ def _on_run_update(self, run: Run) -> None:
521579
outputs = run.outputs or {}
522580

523581
if current_step:
524-
current_step.output = self.process_content(outputs)
582+
if current_step.type != "llm":
583+
current_step.output = self.process_content(outputs)
525584
current_step.end()
526585

527586
def _on_error(self, error: BaseException, *, run_id: "UUID", **kwargs: Any):

0 commit comments

Comments
 (0)