Skip to content

Commit e4a83e0

Browse files
authored
Update chatCompletion.py
1 parent 96e91b8 commit e4a83e0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

mcp_bridge/openai_clients/chatCompletion.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
FinishReason1,
1111
)
1212

13-
from .utils import call_tool, chat_completion_add_tools
13+
from .utils import call_tool, chat_completion_add_tools, validate_if_json_object_parsable, json_pretty_print
1414
from mcp_bridge.http_clients import get_client
1515
from mcp_bridge.inference_engine_mappers.chat.requester import chat_completion_requester
1616
from mcp_bridge.inference_engine_mappers.chat.responder import chat_completion_responder
@@ -86,11 +86,22 @@ async def chat_completions(
8686
return response
8787

8888
logger.debug("tool calls found")
89+
90+
logger.debug("clearing tool contexts to prevent tool call loops")
91+
request.tools = None
92+
8993
for tool_call in response.choices[0].message.tool_calls.root:
9094
logger.debug(
91-
f"tool call: {tool_call.function.name} arguments: {json.loads(tool_call.function.arguments)}"
95+
f"tool call: {tool_call.function.name}"
9296
)
9397

98+
if validate_if_json_object_parsable(tool):
99+
logger.debug(f"arguments:\n{json_pretty_print(tool_call.function.arguments)}")
100+
else:
101+
logger.debug("non-json arguments given: %s" % tool_call.function.arguments)
102+
logger.debug("unable to parse tool call argument as json. skipping...")
103+
continue
104+
94105
# FIXME: this can probably be done in parallel using asyncio gather
95106
tool_call_result = await call_tool(
96107
tool_call.function.name, tool_call.function.arguments

0 commit comments

Comments
 (0)