Skip to content

Commit d54d59e

Browse files
committed
feat(llm): extract reasoning and tool calls from content blocks langchain v1
1 parent 44d7bbb commit d54d59e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

nemoguardrails/actions/llm/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,26 @@ def _extract_and_remove_think_tags(response) -> Optional[str]:
317317

318318
def _store_tool_calls(response) -> None:
319319
"""Extract and store tool calls from response in context."""
320-
tool_calls = getattr(response, "tool_calls", None)
320+
tool_calls = _extract_tool_calls_from_content_blocks(response)
321+
if not tool_calls:
322+
tool_calls = _extract_tool_calls_from_attribute(response)
321323
tool_calls_var.set(tool_calls)
322324

323325

326+
def _extract_tool_calls_from_content_blocks(response) -> List | None:
327+
if hasattr(response, "content_blocks"):
328+
tool_calls = []
329+
for block in response.content_blocks:
330+
if block.get("type") == "tool_call":
331+
tool_calls.append(block)
332+
return tool_calls if tool_calls else None
333+
return None
334+
335+
336+
def _extract_tool_calls_from_attribute(response) -> List | None:
337+
return getattr(response, "tool_calls", None)
338+
339+
324340
def _store_response_metadata(response) -> None:
325341
"""Store response metadata excluding content for metadata preservation.
326342

0 commit comments

Comments
 (0)