fix(agent): Ollama の format に移植スキーマを渡し文法変換失敗を解消#422
Conversation
ローカル Ollama(LLM_LOCAL_OLLAMA=1)経路で /api/chat が "Failed to initialize samplers: failed to parse grammar" の 400 を返し、 Agent チャットが 502 になっていた。原因は format に渡す JSON Schema の maxLength / maxItems を llama.cpp の GBNF 文法変換が解釈できないこと (oneOf は無罪。実機 curl で maxLength/maxItems 除去後は 200 を確認)。 - ollama_client: format に to_portable_schema(..., drop_additional_properties=False) を渡す(maxLength/maxItems 除去・oneOf→enum 平坦化)。Gemini/OpenAI と同じ移植経路に揃える - output_schema: 「Ollama は maxLength を助言扱い/生スキーマを使う」誤コメントを実態へ修正 - test_llm_clients: format に oneOf/maxLength/maxItems が残らないことの回帰テストを追加 文字数上限の実強制は従来どおり _parse_response が担う(二重防衛)。 Anthropic/Gemini/OpenAI 経路は無変更。make lint-backend / make test-backend(633 passed) green。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesOllama Portable Schema
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
概要
ローカル Ollama 経路(
LLM_LOCAL_OLLAMA=1)で Agent チャットが 502 になっていた不具合を修正する。原因
OllamaClientが/api/chatのformatに 生スキーマ(build_output_schema)を渡しており、そのvalue.maxLength(2000) /suggestions.maxItems(4) を llama.cpp の JSON Schema → GBNF 文法変換が解釈できず、{"error":{"message":"Failed to initialize samplers: failed to parse grammar"}}の 400 を返していた。これがLLMError→ 502 になっていた。実機 curl で切り分け済み:
oneOfのみ → 200(無罪)failed to parse grammar)maxLength/maxItemsを除去(to_portable_schema相当)→ 200変更内容
ollama_client.py:formatにto_portable_schema(output_schema, drop_additional_properties=False)を渡す(maxLength/maxItems除去・oneOf→enum平坦化)。Gemini/OpenAI と同じ移植スキーマ経路に揃える。output_schema.py: 「Ollama はmaxLengthを助言扱い/生スキーマをそのまま使う」という誤った前提のコメントを実態(数値制約で文法変換が壊れる)に修正。ロジックは無変更。tests/test_llm_clients.py: 回帰テスト追加。Ollama に投げるformatにoneOf/maxLength/maxItemsが残らず、fieldが enum に畳まれることを検証。影響範囲
chat_service._parse_response(二重防衛)が担うため品質は不変。to_portable_schemaの本体は触らず、Ollama を相乗りさせただけ)。検証
make lint-backend: passmake test-backend: pass(633 passed)/api/chatで 200 を返すことを curl で確認🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests