Home / Function/ _backwards_compat_tool_calls() — langchain Function Reference

_backwards_compat_tool_calls() — langchain Function Reference

Architecture documentation for the _backwards_compat_tool_calls() function in ai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  cd2d4626_48eb_6dac_d882_5d396908cb1f["_backwards_compat_tool_calls()"]
  de5a7878_b3fe_95d7_2575_7f534546dc1e["AIMessage"]
  cd2d4626_48eb_6dac_d882_5d396908cb1f -->|defined in| de5a7878_b3fe_95d7_2575_7f534546dc1e
  style cd2d4626_48eb_6dac_d882_5d396908cb1f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/ai.py lines 304–346

    def _backwards_compat_tool_calls(cls, values: dict) -> Any:
        check_additional_kwargs = not any(
            values.get(k)
            for k in ("tool_calls", "invalid_tool_calls", "tool_call_chunks")
        )
        if check_additional_kwargs and (
            raw_tool_calls := values.get("additional_kwargs", {}).get("tool_calls")
        ):
            try:
                if issubclass(cls, AIMessageChunk):
                    values["tool_call_chunks"] = default_tool_chunk_parser(
                        raw_tool_calls
                    )
                else:
                    parsed_tool_calls, parsed_invalid_tool_calls = default_tool_parser(
                        raw_tool_calls
                    )
                    values["tool_calls"] = parsed_tool_calls
                    values["invalid_tool_calls"] = parsed_invalid_tool_calls
            except Exception:
                logger.debug("Failed to parse tool calls", exc_info=True)

        # Ensure "type" is properly set on all tool call-like dicts.
        if tool_calls := values.get("tool_calls"):
            values["tool_calls"] = [
                create_tool_call(
                    **{k: v for k, v in tc.items() if k not in {"type", "extras"}}
                )
                for tc in tool_calls
            ]
        if invalid_tool_calls := values.get("invalid_tool_calls"):
            values["invalid_tool_calls"] = [
                create_invalid_tool_call(**{k: v for k, v in tc.items() if k != "type"})
                for tc in invalid_tool_calls
            ]

        if tool_call_chunks := values.get("tool_call_chunks"):
            values["tool_call_chunks"] = [
                create_tool_call_chunk(**{k: v for k, v in tc.items() if k != "type"})
                for tc in tool_call_chunks
            ]

        return values

Domain

Subdomains

Frequently Asked Questions

What does _backwards_compat_tool_calls() do?
_backwards_compat_tool_calls() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/ai.py.
Where is _backwards_compat_tool_calls() defined?
_backwards_compat_tool_calls() is defined in libs/core/langchain_core/messages/ai.py at line 304.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free