Skip to content

Commit 631f1f9

Browse files
authored
fix: trim LLM response content before parsing (#322)
Strip leading and trailing whitespace from final LLM response content so parsers receive normalized text. Add a parametrized unit test that covers multiple whitespace patterns.
1 parent f8b7c90 commit 631f1f9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

packages/data-designer-engine/src/data_designer/engine/models/facade.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def generate(
271271
continue # Back to top
272272

273273
# No tool calls remaining to process
274-
response = completion_response.choices[0].message.content or ""
274+
response = (completion_response.choices[0].message.content or "").strip()
275275
reasoning_trace = getattr(completion_response.choices[0].message, "reasoning_content", None)
276276
messages.append(ChatMessage.as_assistant(content=response, reasoning_content=reasoning_trace or None))
277277
curr_num_correction_steps += 1

packages/data-designer-engine/tests/engine/models/test_facade.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ def capture_and_return(*args: Any, **kwargs: Any) -> ModelResponse:
117117
assert captured_messages[0] == expected_messages
118118

119119

120+
@pytest.mark.parametrize(
121+
"raw_content,expected",
122+
[
123+
("\nHello world", "Hello world"),
124+
(" Hello world ", "Hello world"),
125+
("\n\n Hello world\n", "Hello world"),
126+
("Hello world", "Hello world"),
127+
],
128+
)
129+
@patch("data_designer.engine.models.facade.ModelFacade.completion", autospec=True)
130+
def test_generate_strips_response_content(
131+
mock_completion: Any,
132+
stub_model_facade: ModelFacade,
133+
raw_content: str,
134+
expected: str,
135+
) -> None:
136+
"""Response content from the LLM is stripped of leading/trailing whitespace."""
137+
mock_completion.side_effect = lambda *args, **kwargs: StubResponse(StubMessage(content=raw_content))
138+
result, _ = stub_model_facade.generate(prompt="test", parser=lambda x: x)
139+
assert result == expected
140+
141+
120142
def test_model_alias_property(stub_model_facade, stub_model_configs):
121143
assert stub_model_facade.model_alias == stub_model_configs[0].alias
122144

0 commit comments

Comments
 (0)