Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit c1c6f13

Browse files
committed
Workaround for litellm using a strict OpenAI provider for OpenRouter
The problem is that litellm uses the OpenAI provider for talking to OpenRouter, but OpenRouter uses an OpenAI dialect - for example, the OpenAI python API no longer allows you to POST payloads that contain `post`, but those are often used with OpenRouter for FIM. The effect is that FIM payloads from Continue are rejected by litellm. To work around that, we add a FIM normalizer for OpenRouter that moves prompt to messages, like we often do during normalization, but in this normalizer we don't de-normalize but isntead pass on the payload with `messages` to the completion.
1 parent 7d66062 commit c1c6f13

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/codegate/providers/openrouter/provider.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import json
2+
from typing import Dict
23

34
from fastapi import Header, HTTPException, Request
5+
from litellm.types.llms.openai import ChatCompletionRequest
46

57
from codegate.clients.detector import DetectClient
68
from codegate.pipeline.factory import PipelineFactory
79
from codegate.providers.fim_analyzer import FIMAnalyzer
10+
from codegate.providers.normalizer.completion import CompletionNormalizer
811
from codegate.providers.openai import OpenAIProvider
912

1013

14+
class OpenRouterNormalizer(CompletionNormalizer):
15+
def __init__(self):
16+
super().__init__()
17+
18+
def normalize(self, data: Dict) -> ChatCompletionRequest:
19+
return super().normalize(data)
20+
21+
def denormalize(self, data: ChatCompletionRequest) -> Dict:
22+
if data.get("had_prompt_before", False):
23+
del data["had_prompt_before"]
24+
25+
return data
26+
1127
class OpenRouterProvider(OpenAIProvider):
1228
def __init__(self, pipeline_factory: PipelineFactory):
1329
super().__init__(pipeline_factory)
30+
self._fim_normalizer = OpenRouterNormalizer()
1431

1532
@property
1633
def provider_route_name(self) -> str:

0 commit comments

Comments
 (0)