chore(typing): keep the prompt injection call-type fix on the type-discipline ceiling

This commit is contained in:
Shreyansh Jain 2026-10-14 09:25:29 +05:30
parent f67fcdf349
commit f950a5a862
3 changed files with 16 additions and 2 deletions

View file

@ -40,7 +40,9 @@ def get_formatted_prompt(
if isinstance(data["input"], str):
prompt = data["input"]
elif isinstance(data["input"], list):
prompt = "".join(item for item in data["input"] if isinstance(item, str))
for item in data["input"]:
if isinstance(item, str):
prompt += item
elif call_type == "image_generation":
prompt = data["prompt"]
elif call_type == "audio_transcription":

View file

@ -147,7 +147,11 @@ class _OPTIONAL_PromptInjectionDetection(CustomLogger):
return True # Found a highly similar substring
return False # No substring crossed the threshold
def _formatted_prompt(self, data: dict[str, object], call_type: CallTypesLiteral) -> str | None:
def _formatted_prompt(
self,
data: dict[str, object], # mutable-ok: forwarded as-is to get_formatted_prompt, which takes the request dict
call_type: CallTypesLiteral,
) -> str | None:
if call_type in ("completion", "acompletion"):
return get_formatted_prompt(data=data, call_type="completion")
if call_type in ("text_completion", "atext_completion"):

View file

@ -22,3 +22,11 @@ def test_null_tool_calls_are_skipped(call_type: Literal["acompletion", "completi
}
assert get_formatted_prompt(data=data, call_type=call_type) == 'pingpong{"x":1}'
@pytest.mark.parametrize("call_type", ["embedding", "moderation"])
def test_non_string_list_input_is_skipped(call_type: Literal["embedding", "moderation"]) -> None:
"""/v1/embeddings accepts token id arrays, so the input list is not always text."""
data: Final = {"input": ["scan me", 1234, ["nested"], "and me"]}
assert get_formatted_prompt(data=data, call_type=call_type) == "scan meand me"