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

Commit 7c133a4

Browse files
Force denormalization in OpenRouter FIM to have a prompt key
1 parent e353310 commit 7c133a4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/codegate/providers/openrouter/provider.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,32 @@ def normalize(self, data: Dict) -> ChatCompletionRequest:
2222
return super().normalize(data)
2323

2424
def denormalize(self, data: ChatCompletionRequest) -> Dict:
25-
return super().denormalize(data)
25+
"""
26+
Denormalize a FIM OpenRouter request. Force it to be an accepted atext_completion format.
27+
"""
28+
denormalized_data = super().denormalize(data)
29+
# We are forcing atext_completion which expects to have a "prompt" key in the data
30+
# Forcing it in case is not present
31+
if "prompt" in data:
32+
return denormalized_data
33+
custom_prompt = ""
34+
for msg_dict in denormalized_data.get("messages", []):
35+
content_obj = msg_dict.get("content")
36+
if not content_obj:
37+
continue
38+
if isinstance(content_obj, list):
39+
for content_dict in content_obj:
40+
custom_prompt += (
41+
content_dict.get("text", "") if isinstance(content_dict, dict) else ""
42+
)
43+
elif isinstance(content_obj, str):
44+
custom_prompt += content_obj
45+
46+
# Erase the original "messages" key. Replace it by "prompt"
47+
del denormalized_data["messages"]
48+
denormalized_data["prompt"] = custom_prompt
49+
50+
return denormalized_data
2651

2752

2853
class OpenRouterProvider(OpenAIProvider):

0 commit comments

Comments
 (0)