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
5 changes: 4 additions & 1 deletion llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,10 @@ def schemas_dsl_debug(input, multi):
\b
llm schema dsl 'name, age int, bio: their bio'
"""
schema = schema_dsl(input, multi)
try:
schema = schema_dsl(input, multi)
except ValueError as ex:
raise click.ClickException(str(ex))
click.echo(json.dumps(schema, indent=2))


Expand Down
7 changes: 6 additions & 1 deletion llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ def resolve_schema_input(db, schema_input, load_template):
pass
if " " in schema_input.strip() or "," in schema_input:
# Treat it as schema DSL
return schema_dsl(schema_input)
try:
return schema_dsl(schema_input)
except ValueError as ex:
raise click.BadParameter(str(ex))
# Is it a file on disk?
path = pathlib.Path(schema_input)
if path.exists():
Expand Down Expand Up @@ -392,6 +395,8 @@ def schema_dsl(schema_dsl: str, multi: bool = False) -> Dict[str, Any]:

# Process field name and type
field_parts = field_info.strip().split()
if not field_parts:
raise ValueError("Schema field is missing a name before ':'")
field_name = field_parts[0].strip()

# Default type is string
Expand Down
16 changes: 16 additions & 0 deletions tests/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,22 @@ def test_schemas_dsl():
}


def test_schemas_dsl_invalid_empty_field_name():
result = CliRunner().invoke(cli, ["schemas", "dsl", ":foo"], catch_exceptions=False)
assert result.exit_code == 1
assert result.output == "Error: Schema field is missing a name before ':'\n"


def test_schema_using_dsl_invalid_empty_field_name(mock_model):
result = CliRunner().invoke(
cli,
["prompt", "-m", "mock", "--schema", "name, :foo"],
catch_exceptions=False,
)
assert result.exit_code == 2
assert "Invalid value: Schema field is missing a name before ':'" in result.output


@mock.patch.dict(os.environ, {"OPENAI_API_KEY": "X"})
@pytest.mark.parametrize("custom_database_path", (False, True))
def test_llm_prompt_continue_with_database(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ def test_schema_dsl_multi():
}


def test_schema_dsl_rejects_empty_field_name():
with pytest.raises(ValueError, match="missing a name before ':'"):
schema_dsl(":just a description")


@pytest.mark.parametrize(
"text, max_length, normalize_whitespace, keep_end, expected",
[
Expand Down
Loading