mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-26 07:24:18 +02:00
Fix tool result content handling in OpenAI handler
This commit is contained in:
parent
0d8679c0ad
commit
ead4730423
1 changed files with 12 additions and 8 deletions
|
|
@ -171,17 +171,19 @@ class OpenAISamplingHandler(BaseLLMSamplingHandler):
|
|||
elif isinstance(item, TextContent):
|
||||
text_parts.append(item.text)
|
||||
elif isinstance(item, ToolResultContent):
|
||||
# Tool results in a list - add as separate messages
|
||||
result_text = ""
|
||||
# Each tool result becomes a separate tool message
|
||||
content_parts: list[dict[str, str]] = []
|
||||
if item.content:
|
||||
for sub_item in item.content:
|
||||
if isinstance(sub_item, TextContent):
|
||||
result_text += sub_item.text
|
||||
content_parts.append(
|
||||
{"type": "text", "text": sub_item.text}
|
||||
)
|
||||
openai_messages.append(
|
||||
ChatCompletionToolMessageParam(
|
||||
role="tool",
|
||||
tool_call_id=item.toolUseId,
|
||||
content=result_text,
|
||||
content=content_parts if content_parts else "",
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -234,17 +236,19 @@ class OpenAISamplingHandler(BaseLLMSamplingHandler):
|
|||
|
||||
# Handle ToolResultContent (user's tool results)
|
||||
if isinstance(content, ToolResultContent):
|
||||
# Extract text from the content list
|
||||
result_text = ""
|
||||
# Extract text parts from the content list
|
||||
result_parts: list[dict[str, str]] = []
|
||||
if content.content:
|
||||
for item in content.content:
|
||||
if isinstance(item, TextContent):
|
||||
result_text += item.text
|
||||
result_parts.append(
|
||||
{"type": "text", "text": item.text}
|
||||
)
|
||||
openai_messages.append(
|
||||
ChatCompletionToolMessageParam(
|
||||
role="tool",
|
||||
tool_call_id=content.toolUseId,
|
||||
content=result_text,
|
||||
content=result_parts if result_parts else "",
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue