Skip to content

helpers

Class info

🛈 DocStrings

Prepare LLM output to be in proper shape and executable.

tools_to_codegen

tools_to_codegen(
    tools: Sequence[Tool], include_docstrings: bool = True
) -> ToolsetCodeGenerator

Create a ToolsetCodeGenerator from a sequence of Tools.

Parameters:

Name Type Description Default
tools Sequence[Tool]

Tools to generate code for

required
include_docstrings bool

Include function docstrings in documentation

True

Returns:

Type Description
ToolsetCodeGenerator

ToolsetCodeGenerator instance

Source code in src/llmling_agent/resource_providers/codemode/helpers.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def tools_to_codegen(
    tools: Sequence[Tool],
    include_docstrings: bool = True,
) -> ToolsetCodeGenerator:
    """Create a ToolsetCodeGenerator from a sequence of Tools.

    Args:
        tools: Tools to generate code for
        include_docstrings: Include function docstrings in documentation

    Returns:
        ToolsetCodeGenerator instance
    """
    from pydantic_ai import RunContext
    from schemez import ToolCodeGenerator, ToolsetCodeGenerator, create_schema

    from llmling_agent.agent.context import AgentContext

    generators = [
        ToolCodeGenerator(
            schema=create_schema(
                t.callable,
                name_override=t.name,
                description_override=t.description,
                mode="openai",
                exclude_types=[AgentContext, RunContext],
            ),
            callable=t.callable,
            name_override=t.name,
        )
        for t in tools
    ]
    return ToolsetCodeGenerator(generators, include_docstrings)

validate_code

validate_code(python_code: str) -> None

Validate code structure and raise ModelRetry for fixable issues.

Source code in src/llmling_agent/resource_providers/codemode/helpers.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def validate_code(python_code: str) -> None:
    """Validate code structure and raise ModelRetry for fixable issues."""
    from pydantic_ai import ModelRetry

    code = python_code.strip()
    try:
        ast.parse(code)
    except SyntaxError as e:
        msg = f"Invalid code syntax: {e}"
        logger.info("Invalid code", code=code)
        raise ModelRetry(msg) from None
    else:
        if "async def main(" not in code:
            logger.info("Code not in async def main()", code=code)
            msg = (
                "Code must be wrapped in 'async def main():' function. "
                "Please rewrite your code like:\n"
                "async def main():\n"
                "    # your code here\n"
                "    return result"
            )
            raise ModelRetry(msg)

        # Check if code contains a return statement
        if "return " not in code:
            logger.info("Code does not contain a return statement", code=code)
            msg = "The main() function should return a value."
            raise ModelRetry(msg)