Home / Function/ _construct_responses_api_input() — langchain Function Reference

_construct_responses_api_input() — langchain Function Reference

Architecture documentation for the _construct_responses_api_input() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b988bc7d_ceff_06f1_193c_a22abc7a149f["_construct_responses_api_input()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  a129387a_a0b1_9985_0b43_bfc1f161529a["_construct_responses_api_payload()"]
  a129387a_a0b1_9985_0b43_bfc1f161529a -->|calls| b988bc7d_ceff_06f1_193c_a22abc7a149f
  fd643003_13df_3a67_6bdc_07576981e414["_convert_message_to_dict()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| fd643003_13df_3a67_6bdc_07576981e414
  83f2962a_a930_da72_3b90_e0aa308605d3["_make_computer_call_output_from_message()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| 83f2962a_a930_da72_3b90_e0aa308605d3
  19a91a7c_09b7_1e12_cab8_29322483cdfd["_make_custom_tool_output_from_message()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| 19a91a7c_09b7_1e12_cab8_29322483cdfd
  b1594e89_965d_0f6c_e9b0_511a58b3e737["_ensure_valid_tool_message_content()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| b1594e89_965d_0f6c_e9b0_511a58b3e737
  00006c95_8bb4_f0a5_7955_81d9e07c009d["_format_annotation_from_lc()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| 00006c95_8bb4_f0a5_7955_81d9e07c009d
  ad1a549b_b34e_2769_401e_132731113e67["_pop_index_and_sub_index()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| ad1a549b_b34e_2769_401e_132731113e67
  57d40c25_4875_99e8_1bae_5dd1aad3c2b3["_convert_chat_completions_blocks_to_responses()"]
  b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| 57d40c25_4875_99e8_1bae_5dd1aad3c2b3
  style b988bc7d_ceff_06f1_193c_a22abc7a149f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 4106–4273

def _construct_responses_api_input(messages: Sequence[BaseMessage]) -> list:
    """Construct the input for the OpenAI Responses API."""
    input_ = []
    for lc_msg in messages:
        if isinstance(lc_msg, AIMessage):
            lc_msg = _convert_from_v03_ai_message(lc_msg)
            msg = _convert_message_to_dict(lc_msg, api="responses")
            if isinstance(msg.get("content"), list) and all(
                isinstance(block, dict) for block in msg["content"]
            ):
                tcs: list[types.ToolCall] = [
                    {
                        "type": "tool_call",
                        "name": tool_call["name"],
                        "args": tool_call["args"],
                        "id": tool_call.get("id"),
                    }
                    for tool_call in lc_msg.tool_calls
                ]
                msg["content"] = _convert_from_v1_to_responses(msg["content"], tcs)
        else:
            msg = _convert_message_to_dict(lc_msg, api="responses")
            # Get content from non-standard content blocks
            if isinstance(msg["content"], list):
                for i, block in enumerate(msg["content"]):
                    if isinstance(block, dict) and block.get("type") == "non_standard":
                        msg["content"][i] = block["value"]
        # "name" parameter unsupported
        if "name" in msg:
            msg.pop("name")
        if msg["role"] == "tool":
            tool_output = msg["content"]
            computer_call_output = _make_computer_call_output_from_message(
                cast(ToolMessage, lc_msg)
            )
            custom_tool_output = _make_custom_tool_output_from_message(lc_msg)  # type: ignore[arg-type]
            if computer_call_output:
                input_.append(computer_call_output)
            elif custom_tool_output:
                input_.append(custom_tool_output)
            else:
                tool_output = _ensure_valid_tool_message_content(tool_output)
                function_call_output = {
                    "type": "function_call_output",
                    "output": tool_output,
                    "call_id": msg["tool_call_id"],
                }
                input_.append(function_call_output)
        elif msg["role"] == "assistant":
            if isinstance(msg.get("content"), list):
                for block in msg["content"]:
                    if isinstance(block, dict) and (block_type := block.get("type")):
                        # Aggregate content blocks for a single message
                        if block_type in ("text", "output_text", "refusal"):
                            msg_id = block.get("id")
                            if block_type in ("text", "output_text"):
                                # Defensive check: block may not have "text" key
                                text = block.get("text")
                                if text is None:
                                    # Skip blocks without text content
                                    continue
                                new_block = {
                                    "type": "output_text",
                                    "text": text,
                                    "annotations": [
                                        _format_annotation_from_lc(annotation)
                                        for annotation in block.get("annotations") or []
                                    ],
                                }
                            elif block_type == "refusal":
                                new_block = {
                                    "type": "refusal",
                                    "refusal": block["refusal"],
                                }
                            for item in input_:
                                if (item_id := item.get("id")) and item_id == msg_id:
                                    # If existing block with this ID, append to it
                                    if "content" not in item:
                                        item["content"] = []
                                    item["content"].append(new_block)
                                    break

Domain

Subdomains

Frequently Asked Questions

What does _construct_responses_api_input() do?
_construct_responses_api_input() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _construct_responses_api_input() defined?
_construct_responses_api_input() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 4106.
What does _construct_responses_api_input() call?
_construct_responses_api_input() calls 7 function(s): _convert_chat_completions_blocks_to_responses, _convert_message_to_dict, _ensure_valid_tool_message_content, _format_annotation_from_lc, _make_computer_call_output_from_message, _make_custom_tool_output_from_message, _pop_index_and_sub_index.
What calls _construct_responses_api_input()?
_construct_responses_api_input() is called by 1 function(s): _construct_responses_api_payload.

Analyze Your Own Codebase

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

Try Supermodel Free