Repository navigation
fix: deserialize string tool-call arguments before sending to Ollama - #52253
Conversation
Greptile SummaryThis PR fixes a real-world bug where tool-call Changes:
Minor note: When the input string is valid JSON but not an object (e.g. a bare string, array, or number), or when it is not valid JSON at all, the function falls back silently to Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/ollama-stream.ts
Line: 265-267
Comment:
**Silent data loss on invalid JSON string**
When `value` is a non-empty string that fails `JSON.parse` (e.g. a plain word or a malformed object), the function silently discards it and returns `{}`. The tool call then arrives at Ollama with no arguments at all — which may be equally confusing to debug as the original error.
Consider at least emitting a warning so that misconfigurations are surfaced rather than silently swallowed:
```suggestion
} catch {
// not valid JSON — log and fall through to empty object
console.warn("[ollama] ensureArgsObject: could not parse tool-call arguments as JSON, falling back to {}:", value);
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: deserialize string tool-call argume..." | Re-trigger Greptile |
| } catch { | ||
| // not valid JSON — fall through to empty object | ||
| } |
There was a problem hiding this comment.
Silent data loss on invalid JSON string
When value is a non-empty string that fails JSON.parse (e.g. a plain word or a malformed object), the function silently discards it and returns {}. The tool call then arrives at Ollama with no arguments at all — which may be equally confusing to debug as the original error.
Consider at least emitting a warning so that misconfigurations are surfaced rather than silently swallowed:
| } catch { | |
| // not valid JSON — fall through to empty object | |
| } | |
| } catch { | |
| // not valid JSON — log and fall through to empty object | |
| console.warn("[ollama] ensureArgsObject: could not parse tool-call arguments as JSON, falling back to {}:", value); | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/ollama-stream.ts
Line: 265-267
Comment:
**Silent data loss on invalid JSON string**
When `value` is a non-empty string that fails `JSON.parse` (e.g. a plain word or a malformed object), the function silently discards it and returns `{}`. The tool call then arrives at Ollama with no arguments at all — which may be equally confusing to debug as the original error.
Consider at least emitting a warning so that misconfigurations are surfaced rather than silently swallowed:
```suggestion
} catch {
// not valid JSON — log and fall through to empty object
console.warn("[ollama] ensureArgsObject: could not parse tool-call arguments as JSON, falling back to {}:", value);
}
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4b8fc03aa7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| function ensureArgsObject(value: unknown): Record<string, unknown> { | ||
| if (typeof value === "string") { | ||
| try { | ||
| const parsed = JSON.parse(value); |
There was a problem hiding this comment.
Parse round-tripped tool args without losing large integers
ensureArgsObject decodes stringified tool arguments with raw JSON.parse, which rounds integers above Number.MAX_SAFE_INTEGER (for example 1234567890123456789 becomes 1234567890123456800). That silently corrupts replayed tool-call arguments before they are sent back to Ollama, so any tool relying on 64-bit numeric IDs can receive the wrong target/value. This file already preserves unsafe integers for streamed Ollama JSON via parseJsonPreservingUnsafeIntegers, so this new decode path should do the same to avoid data corruption.
Useful? React with 👍 / 👎.
|
Update — scope clarification: This fix applies to the local Ollama API path ( Ollama Cloud models (routed through the Ollama cloud API) appear to use a different code path and still exhibit tool-calling issues (500 errors when context is large + tool results are in the conversation history). That may be a separate bug or a server-side Ollama Cloud limitation — haven't traced it yet. Wanted to be transparent about the scope so reviewers know exactly what this PR covers and what it doesn't. |
|
Added cloud Ollama fix (commit 3b0a78e) The cloud Ollama path routes through Changes:
This PR now fixes both the local Ollama path ( |
3b0a78e to
53f5103
Compare
|
Landed via temp rebase onto main.
Thanks @Adam-Researchh! |
Summary
When tool calls round-trip through OpenAI-format storage,
argumentsis serialized as a JSON string (OpenAI chat completions format). When this conversation history is later sent back to Ollama viaconvertToOllamaMessages(),extractToolCalls()passesargumentsas-is (string). Ollama's native API expects an object and rejects it with:Result: After a successful tool execution, the model returns empty content on the next turn — the user sees no response.
Changes
ensureArgsObject()helper inollama-stream.tsthat safely parses JSON string arguments back to objectsextractToolCalls()for bothtoolCallandtool_usecontent blocksTesting
ollama-stream.test.tsFixes #50689