Home / Function/ _get_request_payload() — langchain Function Reference

_get_request_payload() — langchain Function Reference

Architecture documentation for the _get_request_payload() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  954f0cfe_731b_ac1b_0145_b5a3d210030b["_get_request_payload()"]
  977b57b2_5d0e_bcf4_a43e_b52857105005["ChatAnthropic"]
  954f0cfe_731b_ac1b_0145_b5a3d210030b -->|defined in| 977b57b2_5d0e_bcf4_a43e_b52857105005
  d6746b81_ab7c_4b02_8921_f885e52e303e["_stream()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 954f0cfe_731b_ac1b_0145_b5a3d210030b
  8028f890_4975_51cc_2409_45db9b39971a["_astream()"]
  8028f890_4975_51cc_2409_45db9b39971a -->|calls| 954f0cfe_731b_ac1b_0145_b5a3d210030b
  cfc69e00_d0bd_d481_d810_57dcf2ca7d10["_generate()"]
  cfc69e00_d0bd_d481_d810_57dcf2ca7d10 -->|calls| 954f0cfe_731b_ac1b_0145_b5a3d210030b
  417dddb4_df70_f3d8_3ca3_1a8f1b4ef59e["_agenerate()"]
  417dddb4_df70_f3d8_3ca3_1a8f1b4ef59e -->|calls| 954f0cfe_731b_ac1b_0145_b5a3d210030b
  a1122c23_c68d_f096_a90e_42f0e4d39d61["_format_messages()"]
  954f0cfe_731b_ac1b_0145_b5a3d210030b -->|calls| a1122c23_c68d_f096_a90e_42f0e4d39d61
  aab782ca_4f87_29fb_27bf_1efa17a219a4["_collect_code_execution_tool_ids()"]
  954f0cfe_731b_ac1b_0145_b5a3d210030b -->|calls| aab782ca_4f87_29fb_27bf_1efa17a219a4
  2e7e015c_db4c_1a62_b5dd_3a0aac0d665c["_is_code_execution_related_block()"]
  954f0cfe_731b_ac1b_0145_b5a3d210030b -->|calls| 2e7e015c_db4c_1a62_b5dd_3a0aac0d665c
  5bd44152_3323_2e97_ed23_07de137480d2["_convert_to_anthropic_output_config_format()"]
  954f0cfe_731b_ac1b_0145_b5a3d210030b -->|calls| 5bd44152_3323_2e97_ed23_07de137480d2
  style 954f0cfe_731b_ac1b_0145_b5a3d210030b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1056–1245

    def _get_request_payload(
        self,
        input_: LanguageModelInput,
        *,
        stop: list[str] | None = None,
        **kwargs: dict,
    ) -> dict:
        """Get the request payload for the Anthropic API."""
        messages = self._convert_input(input_).to_messages()

        for idx, message in enumerate(messages):
            # Translate v1 content
            if (
                isinstance(message, AIMessage)
                and message.response_metadata.get("output_version") == "v1"
            ):
                tcs: list[types.ToolCall] = [
                    {
                        "type": "tool_call",
                        "name": tool_call["name"],
                        "args": tool_call["args"],
                        "id": tool_call.get("id"),
                    }
                    for tool_call in message.tool_calls
                ]
                messages[idx] = message.model_copy(
                    update={
                        "content": _convert_from_v1_to_anthropic(
                            cast(list[types.ContentBlock], message.content),
                            tcs,
                            message.response_metadata.get("model_provider"),
                        )
                    }
                )

        system, formatted_messages = _format_messages(messages)

        # If cache_control is provided in kwargs, add it to the last eligible message
        # block (Anthropic requires cache_control to be nested within a message block).
        # Skip blocks related to code_execution as they cannot have cache_control.
        cache_control = kwargs.pop("cache_control", None)
        if cache_control and formatted_messages:
            # Collect tool IDs called by code_execution
            code_execution_tool_ids = _collect_code_execution_tool_ids(
                formatted_messages
            )

            cache_applied = False
            for formatted_message in reversed(formatted_messages):
                if cache_applied:
                    break
                content = formatted_message.get("content")
                if isinstance(content, list) and content:
                    # Find last eligible block (not code_execution related)
                    for block in reversed(content):
                        if isinstance(block, dict):
                            if _is_code_execution_related_block(
                                block, code_execution_tool_ids
                            ):
                                continue
                            block["cache_control"] = cache_control
                            cache_applied = True
                            break
                elif isinstance(content, str):
                    formatted_message["content"] = [
                        {
                            "type": "text",
                            "text": content,
                            "cache_control": cache_control,
                        }
                    ]
                    cache_applied = True
            # If we didn't find an eligible block we silently drop the control.
            # Anthropic would reject a payload with cache_control on
            # code_execution blocks.
        payload = {
            "model": self.model,
            "max_tokens": self.max_tokens,
            "messages": formatted_messages,
            "temperature": self.temperature,
            "top_k": self.top_k,

Domain

Subdomains

Frequently Asked Questions

What does _get_request_payload() do?
_get_request_payload() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is _get_request_payload() defined?
_get_request_payload() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1056.
What does _get_request_payload() call?
_get_request_payload() calls 4 function(s): _collect_code_execution_tool_ids, _convert_to_anthropic_output_config_format, _format_messages, _is_code_execution_related_block.
What calls _get_request_payload()?
_get_request_payload() is called by 4 function(s): _agenerate, _astream, _generate, _stream.

Analyze Your Own Codebase

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

Try Supermodel Free