Skip to content

Commit bce7a8e

Browse files
authored
feat: openrouter fix (#143)
1 parent 7c2adf7 commit bce7a8e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/ii_agent/llm/openai.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ def generate(
110110
role = "user"
111111
elif str(type(internal_message)) == str(TextResult):
112112
internal_message = cast(TextResult, internal_message)
113-
# For TextResult (assistant), content is handled differently by OpenAI API
114-
message_content_obj = {"type": "text", "text": internal_message.text}
115-
openai_message = {"role": "assistant", "content": [message_content_obj]}
113+
# For TextResult (assistant), OpenAI expects content as a string for regular messages
114+
openai_message = {"role": "assistant", "content": internal_message.text}
116115
openai_messages.append(openai_message)
117116
continue # Move to next message in outer loop
118117
elif str(type(internal_message)) == str(ToolCall):
@@ -163,8 +162,8 @@ def generate(
163162
final_text_for_user_message = f"{system_prompt}\n\n{current_message_text}"
164163
system_prompt_applied = True # Mark as applied
165164

166-
message_content_obj = {"type": "text", "text": final_text_for_user_message}
167-
openai_message = {"role": role, "content": [message_content_obj]}
165+
# For regular text messages, OpenAI expects content as a string
166+
openai_message = {"role": role, "content": final_text_for_user_message}
168167
openai_messages.append(openai_message)
169168

170169
# If cot_model is True and system_prompt was provided but not applied (e.g., no user messages found, though unlikely for an agent)
@@ -173,7 +172,7 @@ def generate(
173172
# Or, one might argue it's an error condition for COT if no user prompt exists.
174173
# For now, let's log a warning and add it as a user message, as some COT models might expect user turn for instructions.
175174
logger.warning("COT mode: System prompt provided but no initial user message to prepend to. Adding as a separate user message.")
176-
openai_messages.insert(0, {"role": "user", "content": [{"type": "text", "text": system_prompt}]})
175+
openai_messages.insert(0, {"role": "user", "content": system_prompt})
177176

178177
# Turn tool_choice into OpenAI tool_choice format
179178
if tool_choice is None:
@@ -299,8 +298,10 @@ def generate(
299298

300299
if content: # Changed from elif due to issue 134
301300
internal_messages.append(TextResult(text=content))
302-
#else:
303-
# raise ValueError(f"Unknown message type: {openai_response_message}")
301+
302+
if not content and not tool_calls:
303+
logger.warning(f"Response has no content or tool_calls: {openai_response_message}")
304+
internal_messages.append(TextResult(text=""))
304305

305306
assert response.usage is not None
306307
message_metadata = {

0 commit comments

Comments
 (0)