Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def call_llm_for_system_prompt(
reasoning_content_seen = False
content_tokens_seen = 0
for chunk in current_request:
if not getattr(chunk, "choices", None):
logger.debug("Skipping streaming chunk without choices during prompt generation")
continue

delta = chunk.choices[0].delta
reasoning_content = getattr(delta, "reasoning_content", None)
new_token = delta.content
Expand Down
28 changes: 28 additions & 0 deletions test/backend/utils/test_llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ def test_call_llm_for_system_prompt_exception(self, mocker: MockFixture):
# Verify AppException is raised with correct error code for unmapped errors
assert exc_info.value.error_code == ErrorCode.MODEL_PROMPT_GENERATION_FAILED

def test_call_llm_for_system_prompt_skips_empty_choices_chunks(self, mocker: MockFixture):
mock_get_model_by_id = mocker.patch('backend.utils.llm_utils.get_model_by_model_id')
mock_get_model_name = mocker.patch('backend.utils.llm_utils.get_model_name_from_config')
mock_openai = mocker.patch('backend.utils.llm_utils.OpenAIModel')

mock_get_model_by_id.return_value = {"base_url": "https://example.com", "api_key": "k"}
mock_get_model_name.return_value = "deepseek-ai/DeepSeek-V4-Flash"

empty_choices_chunk = MagicMock()
empty_choices_chunk.choices = []

content_chunk = MagicMock()
content_chunk.choices = [MagicMock()]
content_chunk.choices[0].delta.content = "Generated prompt"

mock_instance = mock_openai.return_value
mock_instance.client = MagicMock()
mock_instance.client.chat.completions.create.return_value = [
empty_choices_chunk,
content_chunk,
]
mock_instance._prepare_completion_kwargs.return_value = {}

res = call_llm_for_system_prompt(1, "u", "s")

assert res == "Generated prompt"



class TestProcessThinkingTokens:
def test_process_thinking_tokens_normal_token(self):
Expand Down