Skip to content

Commit 53ef636

Browse files
authored
feat: tool function parameters can now include a leading underscore (#174)
* feat: tool function parameters may now include a leading underscore * Fix lint; update changelog
1 parent 6891bfc commit 53ef636

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### New features
1717

1818
* `ChatAuto()`'s new `provider_model` takes both provider and model in a single string in the format `"{provider}/{model}"`, e.g. `"openai/gpt-5"`. If not provided, `ChatAuto()` looks for the `CHATLAS_CHAT_PROVIDER_MODEL` environment variable, defaulting to `"openai"` if neither are provided. Unlike previous versions of `ChatAuto()`, the environment variables are now used *only if function arguments are not provided*. In other words, if `provider_model` is given, the `CHATLAS_CHAT_PROVIDER_MODEL` environment variable is ignored. Similarly, `CHATLAS_CHAT_ARGS` are only used if no `kwargs` are provided. This improves interactive use cases, makes it easier to introduce application-specific environment variables, and puts more control in the hands of the developer. (#159)
19-
* The `.register_tool()` method now accepts a `Tool` instance as input. This is primarily useful for binding things like `annotations` to the `Tool` in one place, and registering it in another. (#172)
19+
* The `.register_tool()` method now:
20+
* Accepts a `Tool` instance as input. This is primarily useful for binding things like `annotations` to the `Tool` in one place, and registering it in another. (#172)
21+
* Supports function parameter names that start with an underscore. (#174)
2022
* The `ToolAnnotations` type gains an `extra` key field -- providing a place for providing additional information that other consumers of tool annotations (e.g., [shinychat](https://posit-dev.github.io/shinychat/)) may make use of.
2123

2224
### Bug fixes

chatlas/_tools.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,21 @@ def func_to_basemodel(func: Callable) -> type[BaseModel]:
326326
)
327327
annotation = Any
328328

329+
# create_model() will error if the field name starts with `_` (since Pydantic
330+
# uses this to indicate private fields). We can work around this by using an alias.
331+
alias = None
332+
if name.startswith("_"):
333+
field_name, alias = (name.lstrip("_"), name)
334+
else:
335+
field_name, alias = (name, None)
336+
329337
if param.default != inspect.Parameter.empty:
330-
field = Field(default=param.default)
338+
field = Field(default=param.default, alias=alias)
331339
else:
332-
field = Field()
340+
field = Field(alias=alias)
333341

334342
# Add the field to our fields dict
335-
fields[name] = (annotation, field)
343+
fields[field_name] = (annotation, field)
336344

337345
return create_model(func.__name__, **fields)
338346

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ def assert_tools_parallel(
162162
):
163163
chat = chat_fun(system_prompt="Be very terse, not even punctuation.")
164164

165-
def favorite_color(person: str):
165+
def favorite_color(_person: str):
166166
"""Returns a person's favourite colour"""
167-
return "sage green" if person == "Joe" else "red"
167+
return "sage green" if _person == "Joe" else "red"
168168

169169
chat.register_tool(favorite_color)
170170

0 commit comments

Comments
 (0)