Skip to content

feat: Add comprehensive Ollama integration with JSON repair #1424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

t-chamb
Copy link

@t-chamb t-chamb commented Jun 29, 2025

Pull Request: Add comprehensive Ollama integration with JSON repair

Branch Information

  • Branch name: feat/ollama-json-repair
  • Base branch: main
  • Commits: 4 new commits

PR Title

feat: Add comprehensive Ollama integration with JSON repair

PR Description

Summary

This PR adds comprehensive Ollama support to Codex with robust handling of malformed JSON responses. Ollama models can now execute shell commands, apply patches, and use other Codex tools reliably.

Key Features

1. Dynamic JSON Repair System

  • Automatically repairs common JSON issues from Ollama:
    • Invalid escape sequences (e.g., \(\\()
    • Trailing commas
    • Missing quotes around property names
    • Single quotes instead of double quotes
    • Incomplete JSON (missing closing braces)

2. Multi-line JSON Parsing

  • Implements brace-counting parser to handle JSON split across multiple lines
  • Consistent parsing between streaming response handler and UI layer
  • Handles complex nested structures

3. Ollama-Specific Enhancements

  • Automatic detection based on baseURL (port 11434)
  • Custom system prompts optimized for Ollama's function calling
  • Special streaming logic to buffer and process complete responses
  • UI filtering to ensure JSON is never visible to users

4. Error Recovery

  • ENOTDIR fix for invalid workdir parameters
  • Three-level fallback system for JSON parsing
  • Graceful degradation when repair fails

Changes

  • New file: repair-json.ts - JSON repair utilities
  • Updated: responses.ts - Ollama detection and multi-line JSON parsing
  • Updated: parsers.ts - JSON repair for tool arguments/output
  • Updated: extract-applied-patches.ts - JSON repair for patch extraction
  • Updated: agent-loop.ts - Ollama-specific system prompts
  • Updated: handle-exec-command.ts - ENOTDIR error handling
  • Updated: exec.ts - Shell built-in detection
  • Updated: terminal-chat-response-item.tsx - Enhanced UI filtering
  • Documentation: Comprehensive OLLAMA_INTEGRATION.md

Testing

Tested with multiple Ollama models including:

  • qwen2.5-coder:32b
  • deepseek-coder-v2
  • llama3.1

All models successfully:

  • Execute shell commands with special characters
  • Apply patches correctly
  • Handle multi-line JSON responses
  • Work with malformed JSON output

Usage

# With Ollama provider
codex --provider ollama --model qwen2.5-coder:32b

# With OpenAI-compatible endpoint
codex --provider openai --base-url http://localhost:11434/v1 --model qwen2.5-coder:32b

Test Plan

  • Basic function calling works with Ollama
  • Malformed JSON is repaired correctly
  • Multi-line JSON is parsed properly
  • No JSON visible in UI during execution
  • ENOTDIR errors are handled gracefully
  • Shell built-ins execute correctly
  • Patches can be extracted from malformed responses

Files Changed (12 files, +763 -65)

  • OLLAMA_INTEGRATION.md (new file, +121)
  • codex-cli/src/components/chat/terminal-chat-response-item.tsx (+88 -6)
  • codex-cli/src/components/model-overlay.tsx (+22 -0)
  • codex-cli/src/utils/agent/agent-loop.ts (+28)
  • codex-cli/src/utils/agent/exec.ts (+12)
  • codex-cli/src/utils/agent/handle-exec-command.ts (+10 -0)
  • codex-cli/src/utils/extract-applied-patches.ts (+11 -0)
  • codex-cli/src/utils/parsers.ts (+23 -0)
  • codex-cli/src/utils/repair-json.ts (new file, +116)
  • codex-cli/src/utils/responses.ts (+351 modifications)
  • codex-rs/core/src/chat_completions.rs (+36 modifications)
  • run-codex.sh (+10)

Todd Chamberlain and others added 3 commits June 28, 2025 22:40
- Allow model switching for local providers (Ollama) during active sessions
- Add setup documentation and helper scripts for Ollama integration
- Distinguish between hosted and local provider switching constraints

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Document current limitations with function/tool calling in Ollama
- Add test script to verify tool support
- Update setup guide with warning about tool limitations
- Provide workarounds and future outlook

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add provider-aware response transformation in chat_completions.rs
- Detect and parse Ollama's text-based function call format
- Transform JSON function calls into proper OpenAI tool_calls structure
- Generate unique call IDs for Ollama function calls
- Update documentation to reflect working tool support
- Add test scripts to verify functionality

This fix enables full tool support for Ollama, including:
- Shell commands via the built-in shell tool
- File operations
- MCP server tools (when configured)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


Todd Chamberlain seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@t-chamb
Copy link
Author

t-chamb commented Jun 29, 2025

I have read the CLA Document and I hereby sign the CLA

This commit adds full Ollama support to Codex with robust handling of
malformed JSON responses:

Features:
- Dynamic JSON repair for common Ollama output issues (invalid escapes,
  trailing commas, missing quotes, incomplete JSON)
- Multi-line JSON parsing with brace-counting algorithm
- Ollama-specific system prompts for better function calling
- UI filtering to hide JSON function calls from display
- ENOTDIR fix for invalid workdir parameters

Changes:
- Add repair-json.ts with JSON repair utilities
- Update responses.ts with Ollama detection and multi-line JSON parsing
- Integrate JSON repair in parsers.ts for tool arguments/output
- Add JSON repair to extract-applied-patches.ts
- Update agent-loop.ts with Ollama-specific prompts
- Fix ENOTDIR error in handle-exec-command.ts
- Add shell built-in detection in exec.ts
- Clean up UI filtering in terminal-chat-response-item.tsx

Testing:
- Tested with qwen2.5-coder:32b and other Ollama models
- Handles malformed JSON with invalid escapes like \( → \\(
- Properly parses multi-line JSON function calls
- No JSON visible in UI during function execution

Documentation:
- Add comprehensive OLLAMA_INTEGRATION.md
- Remove individual test files and documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@t-chamb t-chamb force-pushed the feat/ollama-json-repair branch from af6401e to 6b81406 Compare June 29, 2025 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant