Skip to slide
032 / 170

CH 03 · Building the Prompt · 09/12

Read as one page

Real code: the call_id is copied, not invented

The tool_call and tool_result helpers you just wrote share a call_id because you type the same string in both. Codex copies the string the model already sent.

    pub fn build_tool_call(item: ResponseItem) -> Result<Option<ToolCall>, FunctionCallError> {
        match item {
            ResponseItem::FunctionCall {
                name,
                namespace,
                arguments, // [1]
                encrypted_function_args,
                call_id,
                ..
            } => {
                let tool_name = ToolName::new(namespace, name).with_default_namespace(); // [2]
                Ok(Some(ToolCall {
                    tool_name,
                    call_id, // [3]
                    payload: ToolPayload::Function { arguments },
                    encrypted_function_args,
                }))
            }

Source: openai/codex, codex-rs/core/src/tools/router.rs (Apache-2.0)

After the tool runs, the result is a new history item that carries that same id.

fn function_tool_response(
    call_id: &str,
    payload: &ToolPayload,
    body: Vec<FunctionCallOutputContentItem>,
    success: Option<bool>,
) -> ResponseInputItem {
    let body = match body.as_slice() {
        [FunctionCallOutputContentItem::InputText { text }] => {
            FunctionCallOutputBody::Text(text.clone()) // [4]
        }
        _ => FunctionCallOutputBody::ContentItems(body),
    };

    if matches!(payload, ToolPayload::Custom { .. }) {
        return ResponseInputItem::CustomToolCallOutput {
            call_id: call_id.to_string(), // [5]
            name: None,
            output: FunctionCallOutputPayload { body, success },
        };
    }

    ResponseInputItem::FunctionCallOutput {
        call_id: call_id.to_string(),
        output: FunctionCallOutputPayload { body, success },
    }
}

Source: openai/codex, codex-rs/core/src/tools/context.rs (Apache-2.0)

What to notice

  1. [1] ResponseItem is the enum of history items, and FunctionCall is the variant for a normal tool call. arguments is still a string of JSON here. build_tool_call does not parse it. It moves the string onto ToolCall, the harness's internal request to run one tool. On FunctionCall in protocol/src/models.rs, that field is documented as a string of JSON, parsed by a later step, not by this function.
  2. [2] with_default_namespace fills in "functions" when the model left the namespace empty, so the registry looks up the tool you actually registered. Dropping the namespace would make a namespaced tool look like a different name.
  3. [3] call_id is moved across, not replaced with a new id. Your helpers match because the example passes "c1" twice. Nothing in the builder checks that you did.
  4. [4] A single text chunk is stored as plain text. Any other body, including several chunks or one non-text chunk, stays a list. Either shape is still tied to the call by the id, not by position in the list. That is the three-parallel-shell-commands case from the start of the chapter.
  5. [5] A freeform (custom) payload is answered with CustomToolCallOutput, and a normal payload with FunctionCallOutput. Both copy call_id. Using one output type for every call would record a result whose type does not match the call that asked for it.

Read it yourself: In models.rs, find the FunctionCall and FunctionCallOutput variants. Compare the type of call_id on each (a String on the call, an Option<String> on the stored output). Then search stream_events_utils.rs for response_input_to_response_item and see the Some(call_id.clone()) that fills the output. The line that would break the link is the one that would write None there.

Finished this idea?

Your learning progress stays in this browser.