Skip to content

Build and run agents you can see, understand and trust.

feat(model): pass through the model's response ID (#1333)

qbc46afef293ea0
diff --git a/src/agentscope/agent/_react_agent.py b/src/agentscope/agent/_react_agent.py
index f8033ac67..e229041b7 100644
--- a/src/agentscope/agent/_react_agent.py
+++ b/src/agentscope/agent/_react_agent.py
@@ -584,6 +584,7 @@ class ReActAgent(ReActAgentBase):
msg = Msg(name=self.name, content=[], role="assistant")
if self.model.stream:
async for content_chunk in res:
+ msg.invocation_id = content_chunk.id
msg.content = content_chunk.content
# The speech generated from multimodal (audio) models
@@ -601,6 +602,7 @@ class ReActAgent(ReActAgentBase):
await self.print(msg, False, speech=speech)
else:
+ msg.invocation_id = res.id
msg.content = list(res.content)
if self.tts_model:
@@ -757,6 +759,7 @@ class ReActAgent(ReActAgentBase):
res_msg = Msg(self.name, [], "assistant")
if isinstance(res, AsyncGenerator):
async for chunk in res:
+ res_msg.invocation_id = chunk.id
res_msg.content = chunk.content
# The speech generated from multimodal (audio) models
@@ -774,6 +777,7 @@ class ReActAgent(ReActAgentBase):
await self.print(res_msg, False, speech=speech)
else:
+ res_msg.invocation_id = res.id
res_msg.content = res.content
if self.tts_model:
diff --git a/src/agentscope/model/_anthropic_model.py b/src/agentscope/model/_anthropic_model.py
index 7e99ac63c..1c62fbe0c 100644
--- a/src/agentscope/model/_anthropic_model.py
+++ b/src/agentscope/model/_anthropic_model.py
@@ -348,13 +348,16 @@ class AnthropicChatModel(ChatModelBase):
time=(datetime.now() - start_datetime).total_seconds(),
)
- parsed_response = ChatResponse(
- content=content_blocks,
- usage=usage,
- metadata=metadata,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ response_id = getattr(response, "id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
- return parsed_response
+ return ChatResponse(**resp_kwargs)
async def _parse_anthropic_stream_completion_response(
self,
@@ -386,6 +389,7 @@ class AnthropicChatModel(ChatModelBase):
"""
usage = None
+ response_id: str | None = None
text_buffer = ""
thinking_buffer = ""
thinking_signature = ""
@@ -404,6 +408,8 @@ class AnthropicChatModel(ChatModelBase):
if event.type == "message_start":
message = event.message
+ if response_id is None:
+ response_id = getattr(message, "id", None)
if message.usage:
usage = ChatUsage(
input_tokens=message.usage.input_tokens,
@@ -498,11 +504,14 @@ class AnthropicChatModel(ChatModelBase):
metadata = repaired_input
if contents:
- res = ChatResponse(
- content=contents,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs: dict[str, Any] = {
+ "content": contents,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ res = ChatResponse(**_kwargs)
yield res
last_content = copy.deepcopy(contents)
@@ -519,11 +528,14 @@ class AnthropicChatModel(ChatModelBase):
if structured_model:
metadata = input_obj
- yield ChatResponse(
- content=last_content,
- usage=usage,
- metadata=metadata,
- )
+ _final_kwargs: dict[str, Any] = {
+ "content": last_content,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _final_kwargs["id"] = response_id
+ yield ChatResponse(**_final_kwargs)
def _format_tools_json_schemas(
self,
diff --git a/src/agentscope/model/_dashscope_model.py b/src/agentscope/model/_dashscope_model.py
index f91f7dd11..c37f1dc6f 100644
--- a/src/agentscope/model/_dashscope_model.py
+++ b/src/agentscope/model/_dashscope_model.py
@@ -341,6 +341,7 @@ class DashScopeChatModel(ChatModelBase):
metadata = None
last_content = None
usage = None
+ response_id: str | None = None
async for chunk in giter(response):
if chunk.status_code != HTTPStatus.OK:
@@ -348,6 +349,9 @@ class DashScopeChatModel(ChatModelBase):
f"Failed to get response from _ API: {chunk}",
)
+ if response_id is None:
+ response_id = getattr(chunk, "request_id", None)
+
message = chunk.output.choices[0].message
# Update reasoning content
@@ -446,11 +450,14 @@ class DashScopeChatModel(ChatModelBase):
)
if content_blocks:
- parsed_chunk = ChatResponse(
- content=content_blocks,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ parsed_chunk = ChatResponse(**_kwargs)
yield parsed_chunk
last_content = copy.deepcopy(content_blocks)
@@ -468,11 +475,14 @@ class DashScopeChatModel(ChatModelBase):
if structured_model:
metadata = input_obj
- yield ChatResponse(
- content=last_content,
- usage=usage,
- metadata=metadata,
- )
+ _final_kwargs: dict[str, Any] = {
+ "content": last_content,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _final_kwargs["id"] = response_id
+ yield ChatResponse(**_final_kwargs)
async def _parse_dashscope_generation_response(
self,
@@ -569,13 +579,16 @@ class DashScopeChatModel(ChatModelBase):
metadata=response.usage,
)
- parsed_response = ChatResponse(
- content=content_blocks,
- usage=usage,
- metadata=metadata,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ response_id = getattr(response, "request_id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
- return parsed_response
+ return ChatResponse(**resp_kwargs)
def _format_tools_json_schemas(
self,
diff --git a/src/agentscope/model/_gemini_model.py b/src/agentscope/model/_gemini_model.py
index 18f99fa4e..0a393b69d 100644
--- a/src/agentscope/model/_gemini_model.py
+++ b/src/agentscope/model/_gemini_model.py
@@ -333,6 +333,7 @@ class GeminiChatModel(ChatModelBase):
)
return None
+ # pylint: disable=too-many-branches
async def _parse_gemini_stream_generation_response(
self,
start_datetime: datetime,
@@ -366,6 +367,7 @@ class GeminiChatModel(ChatModelBase):
thinking = ""
tool_calls: list[ToolUseBlock] = []
metadata: dict | None = None
+ response_id: str | None = None
async for chunk in response:
if (
chunk.candidates
@@ -434,11 +436,17 @@ class GeminiChatModel(ChatModelBase):
),
)
- yield ChatResponse(
- content=content_blocks + tool_calls,
- usage=usage,
- metadata=metadata,
- )
+ if response_id is None:
+ response_id = getattr(chunk, "response_id", None)
+
+ _kwargs: dict[str, Any] = {
+ "content": content_blocks + tool_calls,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ yield ChatResponse(**_kwargs)
def _parse_gemini_generation_response(
self,
@@ -527,11 +535,16 @@ class GeminiChatModel(ChatModelBase):
usage = self._extract_usage(response.usage_metadata, start_datetime)
- return ChatResponse(
- content=content_blocks + tool_calls,
- usage=usage,
- metadata=metadata,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks + tool_calls,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ response_id = getattr(response, "response_id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
+
+ return ChatResponse(**resp_kwargs)
def _format_tools_json_schemas(
self,
diff --git a/src/agentscope/model/_ollama_model.py b/src/agentscope/model/_ollama_model.py
index d66060200..6a86fa1a1 100644
--- a/src/agentscope/model/_ollama_model.py
+++ b/src/agentscope/model/_ollama_model.py
@@ -204,6 +204,7 @@ class OllamaChatModel(ChatModelBase):
acc_thinking_content = ""
tool_calls = OrderedDict() # Store tool calls
metadata: dict | None = None
+ response_id: str | None = None
async for chunk in response:
# Handle text content
@@ -262,13 +263,19 @@ class OllamaChatModel(ChatModelBase):
except Exception as e:
print(f"Error parsing tool call input: {e}")
+ if response_id is None:
+ response_id = getattr(chunk, "id", None)
+
# Generate response when there's new content or at final chunk
if chunk.done or contents:
- res = ChatResponse(
- content=contents,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs: dict[str, Any] = {
+ "content": contents,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ res = ChatResponse(**_kwargs)
yield res
async def _parse_ollama_completion_response(
@@ -339,13 +346,16 @@ class OllamaChatModel(ChatModelBase):
time=(datetime.now() - start_datetime).total_seconds(),
)
- parsed_response = ChatResponse(
- content=content_blocks,
- usage=usage,
- metadata=metadata,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ response_id = getattr(response, "id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
- return parsed_response
+ return ChatResponse(**resp_kwargs)
def _format_tools_json_schemas(
self,
diff --git a/src/agentscope/model/_openai_model.py b/src/agentscope/model/_openai_model.py
index 81179d62c..19752c1fa 100644
--- a/src/agentscope/model/_openai_model.py
+++ b/src/agentscope/model/_openai_model.py
@@ -336,6 +336,7 @@ class OpenAIChatModel(ChatModelBase):
will be stored in the metadata of the `ChatResponse`.
"""
usage, res = None, None
+ response_id: str | None = None
text = ""
thinking = ""
audio = ""
@@ -356,6 +357,9 @@ class OpenAIChatModel(ChatModelBase):
else:
chunk = item
+ if response_id is None:
+ response_id = getattr(chunk, "id", None)
+
if chunk.usage:
usage = ChatUsage(
input_tokens=chunk.usage.prompt_tokens,
@@ -366,11 +370,14 @@ class OpenAIChatModel(ChatModelBase):
if not chunk.choices:
if usage and contents:
- res = ChatResponse(
- content=contents,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs: dict[str, Any] = {
+ "content": contents,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ res = ChatResponse(**_kwargs)
yield res
continue
@@ -482,11 +489,14 @@ class OpenAIChatModel(ChatModelBase):
)
if contents:
- res = ChatResponse(
- content=contents,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs = {
+ "content": contents,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ res = ChatResponse(**_kwargs)
yield res
last_contents = copy.deepcopy(contents)
@@ -503,11 +513,14 @@ class OpenAIChatModel(ChatModelBase):
if structured_model:
metadata = input_obj
- yield ChatResponse(
- content=last_contents,
- usage=usage,
- metadata=metadata,
- )
+ _kwargs = {
+ "content": last_contents,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ yield ChatResponse(**_kwargs)
def _parse_openai_completion_response(
self,
@@ -611,13 +624,16 @@ class OpenAIChatModel(ChatModelBase):
metadata=response.usage,
)
- parsed_response = ChatResponse(
- content=content_blocks,
- usage=usage,
- metadata=metadata,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "usage": usage,
+ "metadata": metadata,
+ }
+ response_id = getattr(response, "id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
- return parsed_response
+ return ChatResponse(**resp_kwargs)
def _format_tools_json_schemas(
self,