Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 115 additions & 9 deletions areal/experimental/openai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
from areal.api import ModelRequest, ModelResponse
from areal.api.cli_args import GenerationHyperparameters
from areal.experimental.openai.cache import InteractionCache
from areal.experimental.openai.prompt_renderer import (
IncrementalPromptRenderer,
tools_signature,
)
from areal.experimental.openai.tool_call_parser import process_tool_calls
from areal.experimental.openai.types import InteractionWithTokenLogpReward
from areal.utils import logging
Expand Down Expand Up @@ -672,6 +676,26 @@ def _concat_prompt_token_ids_with_parent(
all_message_list = _parse_tool_call_arguments(all_message_list)

if full_prompt_token_ids is None:
if (
parent is not None
and parent.output_message_list is not None
and IncrementalPromptRenderer.is_supported(
tokenizer,
tools=tools,
chat_template_kwargs=extra_body.get("chat_template_kwargs", {}),
)
):
child_tokens = IncrementalPromptRenderer.render_concat_child_tokens(
tokenizer,
parent_output_messages=parent.output_message_list,
message_list=message_list,
tools=tools,
chat_template_kwargs=extra_body.get("chat_template_kwargs", {}),
)
if child_tokens is not None:
prompt_token_ids = parent_tokens + child_tokens
return prompt_token_ids, len(parent_tokens) - 1, len(parent_tokens)

all_tokens = apply_chat_template(
tokenizer,
all_message_list,
Expand Down Expand Up @@ -713,6 +737,7 @@ async def _prepare_prompt(
tools: Iterable[ChatCompletionToolParam] | None,
extra_body: Body,
require_multimodal_processor: bool = False,
interaction: InteractionWithTokenLogpReward | None = None,
) -> _PreparedPrompt:
"""Prepare text or multimodal prompt data for one agent interaction."""
chat_template_kwargs = extra_body.get("chat_template_kwargs", {})
Expand All @@ -734,18 +759,97 @@ async def _prepare_prompt(
)

if chat_template_type == "hf":
input_ids = (
processed_prompt.input_ids
if processed_prompt is not None
else apply_chat_template(
if processed_prompt is not None:
input_ids = processed_prompt.input_ids
elif (
processor is None
and parent is not None
and parent.messages
and len(tokenizer_messages) > len(parent.messages)
and IncrementalPromptRenderer.is_supported(
tokenizer, tools=tools, chat_template_kwargs=chat_template_kwargs
)
):
delta_messages = tokenizer_messages[len(parent.messages) :]
current_tools_signature = tools_signature(tools)
parent_base = parent.prompt_base_token_ids
parent_tools_signature = parent.prompt_tools_signature
if parent_base is None:
parent_base = apply_chat_template(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the tools included in the second round of requests are inconsistent with those in the first round, applying only to the incremental part here will lead to a discrepancy in content between the two.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. render_incremental accepted a tools argument but never passed it through to apply_chat_template, so the tool block was whatever the first turn baked into the prefix. Reproduced on Qwen3-0.6B: 1 tool on turn 1 and 2 tools on turn 2 gives 142 tokens incrementally against 178 for the full render, with the second tool's signature missing entirely.

Fixed by recording the tool set the prefix was rendered with (tools_signature(), stored as InteractionWithTokenLogpReward.prompt_tools_signature) and falling back to full-history rendering when it differs from the current turn's. Covered by a parametrized test over tools added, removed, cleared, and newly introduced between rounds.

tokenizer,
parent.messages,
tools=tools,
add_generation_prompt=False,
tokenize=True,
**chat_template_kwargs,
)
parent.prompt_base_token_ids = parent_base
# Re-rendered here with the current tool set, so it is up to date.
parent_tools_signature = current_tools_signature
parent.prompt_tools_signature = parent_tools_signature
rendered = IncrementalPromptRenderer.render_incremental(
tokenizer,
tokenizer_messages,
parent_base,
delta_messages,
tools=tools,
add_generation_prompt=True,
tokenize=True,
**chat_template_kwargs,
chat_template_kwargs=chat_template_kwargs,
parent_tools_signature=parent_tools_signature,
parent_messages=parent.messages,
)
)
if rendered is not None:
input_ids, new_base = rendered
if interaction is not None:
interaction.prompt_base_token_ids = new_base
interaction.prompt_tools_signature = current_tools_signature
interaction.prompt_token_ids = list(input_ids)
else:
input_ids = apply_chat_template(
tokenizer,
tokenizer_messages,
tools=tools,
add_generation_prompt=True,
tokenize=True,
**chat_template_kwargs,
)
if interaction is not None:
interaction.prompt_token_ids = list(input_ids)
else:
# Rendering the base prefix costs an extra chat-template pass, so only
# pay for it when a later turn could actually reuse it.
if (
processor is None
and IncrementalPromptRenderer.is_supported(
tokenizer, tools=tools, chat_template_kwargs=chat_template_kwargs
)
and IncrementalPromptRenderer.is_history_safe(
tokenizer,
tokenizer_messages,
tools=tools,
chat_template_kwargs=chat_template_kwargs,
)
):
input_ids, base_ids = IncrementalPromptRenderer.render_initial(
tokenizer,
tokenizer_messages,
tools=tools,
chat_template_kwargs=chat_template_kwargs,
)
if interaction is not None:
interaction.prompt_base_token_ids = base_ids
interaction.prompt_tools_signature = tools_signature(tools)
interaction.prompt_token_ids = list(input_ids)
else:
input_ids = apply_chat_template(
tokenizer,
tokenizer_messages,
tools=tools,
add_generation_prompt=True,
tokenize=True,
**chat_template_kwargs,
)
if interaction is not None:
interaction.prompt_token_ids = list(input_ids)

if processor is None:
return _PreparedPrompt(input_ids=input_ids)
return _PreparedPrompt(
Expand Down Expand Up @@ -1051,6 +1155,7 @@ async def create(
tools=tools_list,
extra_body=extra_body,
require_multimodal_processor=self.require_multimodal_processor,
interaction=interaction,
)
prompt_token_ids = prepared_prompt.input_ids
if interaction is not None and self.processor is not None:
Expand Down Expand Up @@ -1508,6 +1613,7 @@ async def create(
tools=tools_list,
extra_body=extra_body,
require_multimodal_processor=self.require_multimodal_processor,
interaction=interaction,
)
prompt_token_ids = prepared_prompt.input_ids
if self.processor is not None:
Expand Down
Loading