tool#
- class baf.reasoning.tool.Tool(fn, name=None, description=None)[source]#
Bases:
objectWraps a Python callable so a reasoning loop can invoke it via JSON-schema args.
The wrapper auto-introspects the callable’s signature, type hints and docstring to build a JSON schema in OpenAI function-calling format, validates LLM-supplied arguments before invocation, and converts the result (and any raised exception) to a string the LLM can read.
- Parameters:
- fn#
the wrapped callable.
- Type:
Callable
- call(args)[source]#
Validate
args, invoke the wrapped callable, and stringify the result.Any exception raised either by validation or by the callable itself is caught and converted to a string of the form
ERROR: <ExceptionType>: <message>so the reasoning loop can read it and recover.
- property openai_schema#
Return the OpenAI function-calling schema for this tool.
- Returns:
a dict of the shape
{"type": "function", "function": {"name", "description", "parameters"}}.- Return type:
- exception baf.reasoning.tool.ToolError[source]#
Bases:
ExceptionRaised when a Tool’s argument validation fails.
The message is intended to be readable by an LLM so it can recover and retry with corrected arguments on the next reasoning step.
- baf.reasoning.tool._annotation_to_schema(annotation, param_name='')[source]#
Map a Python annotation to a minimal JSONSchema property.
- baf.reasoning.tool._build_schema(fn)[source]#
Introspect
fnand build a JSONSchemaparametersobject.The returned dict has the shape:
{"type": "object", "properties": {...}, "required": [...]}
- Parameters:
fn (Callable) – the function to introspect.
- Returns:
the JSONSchema parameters object.
- Return type:
- baf.reasoning.tool._coerce_value(value, json_type, arg_name)[source]#
Validate and lightly coerce
valueto the expected JSONSchemajson_type.- Allowed coercions:
integer accepts strings of digits (optionally signed).
number accepts ints and strings parseable as floats.
boolean accepts the strings “true”/”false” (case-insensitive).
- Raises:
ToolError – if the value cannot be safely coerced.
- baf.reasoning.tool._first_non_empty_line(text)[source]#
Return the first non-empty stripped line of
text(or empty string).
- baf.reasoning.tool._is_optional(annotation)[source]#
Return True if the annotation is an Optional[X] / X | None form.
- Parameters:
annotation – a type annotation as seen by
typing.get_type_hints.- Returns:
True if
Noneis one of the union members, False otherwise.- Return type: