Skip to content

Commit 05ce908

Browse files
committed
Fix ollamacloud api arguments object error
1 parent b8457c0 commit 05ce908

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/messages.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ R_API bool r2ai_msgs_from_json(RList *msgs, const RJson *json) {
308308
return true;
309309
}
310310

311-
R_API char *r2ai_msgs_to_json(const RList *msgs) {
311+
R_API char *r2ai_msgs_to_json(const RList *msgs, bool raw_tool_args) {
312312
if (!msgs || r_list_empty (msgs)) {
313313
return NULL;
314314
}
@@ -372,8 +372,14 @@ R_API char *r2ai_msgs_to_json(const RList *msgs) {
372372
// Add name
373373
pj_ks (pj, "name", tc->name? tc->name: "");
374374

375-
// Add arguments (required by OpenAI API)
376-
pj_ks (pj, "arguments", tc->arguments? tc->arguments: "{}");
375+
// OpenAI wants arguments as a JSON-encoded string; Ollama wants a raw object.
376+
const char *a = R_STR_ISEMPTY (tc->arguments)? "{}": tc->arguments;
377+
pj_k (pj, "arguments");
378+
if (raw_tool_args) {
379+
pj_raw (pj, a);
380+
} else {
381+
pj_s (pj, a);
382+
}
377383

378384
pj_end (pj); // End function object
379385
pj_end (pj); // End tool call object

src/openai.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
9393

9494
if (temp_msgs && !r_list_empty (temp_msgs)) {
9595
R_LOG_DEBUG ("Using input messages: %d messages", r_list_length (temp_msgs));
96-
messages_json = r2ai_msgs_to_json (temp_msgs);
96+
messages_json = r2ai_msgs_to_json (temp_msgs, provider_info && provider_info->api_type == R2AI_API_OLLAMA);
9797
if (!messages_json) {
9898
if (error) {
9999
*error = strdup ("Failed to convert messages to JSON");

src/r2ai.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ R_IPI bool r2ai_init(RCorePluginSession *cps) {
626626

627627
r_config_set_i (core->config, "r2ai.http.timeout", 240);
628628
r_config_desc (core->config, "r2ai.http.timeout", "HTTP client timeout (seconds) for provider API calls");
629-
r_config_set_i (core->config, "r2ai.http.max_retries", 5);
629+
r_config_set_i (core->config, "r2ai.http.max_retries", 3);
630630
r_config_desc (core->config, "r2ai.http.max_retries", "Maximum number of HTTP retries for failed requests");
631631
r_config_set_i (core->config, "r2ai.http.max_backoff", 30);
632632
r_config_desc (core->config, "r2ai.http.max_backoff", "Maximum backoff time (seconds) between HTTP retries");

src/r2ai.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ R_API bool r2ai_msgs_from_json(RList *msgs, const RJson *json);
219219

220220
/**
221221
* Convert messages array to JSON string
222+
* When raw_tool_args is true, tool_call arguments are emitted as raw JSON
223+
* objects (Ollama convention) instead of JSON-encoded strings (OpenAI
224+
* convention).
222225
* Caller must free the returned string
223226
*/
224-
R_API char *r2ai_msgs_to_json(const RList *msgs);
227+
R_API char *r2ai_msgs_to_json(const RList *msgs, bool raw_tool_args);
225228

226229
/**
227230
* Convert messages array to Anthropic format JSON string

0 commit comments

Comments
 (0)