Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c192c78
Fresh diff
kiranandcode Jan 30, 2026
ea3d25c
remove instructionhandler
eb8680 Jan 16, 2026
f83d312
updated internal interface to make all tests pass
kiranandcode Jan 28, 2026
d8d52e7
fixed caching tests
kiranandcode Jan 28, 2026
a322a35
updated llm.ipynb
kiranandcode Jan 28, 2026
41beb78
removed unnecessarily defensive validation
kiranandcode Jan 29, 2026
1400d19
updated tool call decoding to use concrete type of tool result instea…
kiranandcode Jan 29, 2026
a06296d
updated completions to fix basic type errors
kiranandcode Jan 30, 2026
c47abd6
updated call assistant to handle decoding tool calls
kiranandcode Jan 30, 2026
43e5b78
dropped stale comments
kiranandcode Jan 30, 2026
6bb2b13
moved model and param model back to internals of `completions`
kiranandcode Jan 30, 2026
88da657
added default encodable instance for Callable
kiranandcode Jan 30, 2026
88c65ee
fixed type errors
kiranandcode Jan 30, 2026
18da11b
update to use more structured type for synthesis
kiranandcode Jan 31, 2026
52df3f6
updated callable encoding tests
kiranandcode Jan 31, 2026
4b10ac3
s/TypeError/NotImplementedError
kiranandcode Jan 31, 2026
2a8af94
Merge branch 'master' into kg-encodable-default
kiranandcode Jan 31, 2026
2b4449a
simplified smart constructor
kiranandcode Jan 31, 2026
553450f
bare callables not allowed
kiranandcode Jan 31, 2026
aac0eb9
droped synthesis and removed encoding_instructions
kiranandcode Jan 31, 2026
5b3a559
fixed imports
kiranandcode Jan 31, 2026
711b27d
fixed imports and tests
kiranandcode Jan 31, 2026
3f1aa65
added restricted python again
kiranandcode Jan 31, 2026
fd4041e
added test for custom policies for restricted python
kiranandcode Jan 31, 2026
fbe478f
more specific arguments to RestrictedEvalProvider and made exec more …
kiranandcode Jan 31, 2026
6aa4d1b
reverted flags for customizing rglobals, using same rglobals for loca…
kiranandcode Jan 31, 2026
4f0c963
fixed failing tests (env was not being mutated)
kiranandcode Jan 31, 2026
86d8b41
Merge branch 'master' into kg-restricted-eval
kiranandcode Jan 31, 2026
1998d38
updated restricted python to be a llm dependency
kiranandcode Jan 31, 2026
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
85 changes: 85 additions & 0 deletions effectful/handlers/llm/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
from types import CodeType
from typing import Any

from RestrictedPython import (
Eval,
Guards,
RestrictingNodeTransformer,
compile_restricted,
safe_globals,
)

from effectful.ops.syntax import ObjectInterpretation, defop, implements


Expand Down Expand Up @@ -86,3 +94,80 @@ def exec(

# Execute module-style so top-level defs land in `env`.
builtins.exec(bytecode, env, env)


class RestrictedEvalProvider(ObjectInterpretation):
"""
Safer provider using RestrictedPython.

RestrictedPython is not a complete sandbox, but it enforces a restricted
language subset and expects you to provide a constrained exec environment.

policy : dict[str, Any], optional
RestrictedPython compile_restricted policy for compilation
"""

policy: type[RestrictingNodeTransformer] | None = None

def __init__(
self,
*,
policy: type[RestrictingNodeTransformer] | None = None,
):
self.policy = policy

@implements(parse)
def parse(self, source: str, filename: str) -> ast.Module:
# Keep inspect.getsource() working for dynamically-defined objects.
linecache.cache[filename] = (
len(source),
None,
source.splitlines(True),
filename,
)
return ast.parse(source, filename=filename, mode="exec")

@implements(compile)
def compile(self, module: ast.Module, filename: str) -> CodeType:
# RestrictedPython can compile from an AST directly.
return compile_restricted(
module,
filename=filename,
mode="exec",
policy=self.policy or RestrictingNodeTransformer,
)

@implements(exec)
def exec(
self,
bytecode: CodeType,
env: dict[str, Any],
) -> None:
# Build restricted globals from RestrictedPython's defaults
rglobals: dict[str, Any] = safe_globals.copy()

# Enable class definitions (required for Python 3)
rglobals["__metaclass__"] = type
rglobals["__name__"] = "restricted"

# Layer `env` on top (without letting callers replace the restricted builtins).
rglobals.update({k: v for k, v in env.items() if k != "__builtins__"})

# Enable for loops and comprehensions
rglobals["_getiter_"] = Eval.default_guarded_getiter
# Enable sequence unpacking in comprehensions and for loops
rglobals["_iter_unpack_sequence_"] = Guards.guarded_iter_unpack_sequence

rglobals["getattr"] = Guards.safer_getattr
rglobals["setattr"] = Guards.guarded_setattr
rglobals["_write_"] = lambda x: x

# Track keys before execution to identify new definitions
keys_before = set(rglobals.keys())

builtins.exec(bytecode, rglobals, rglobals)

# Copy newly defined items back to env so caller can access them
for key in rglobals:
if key not in keys_before:
env[key] = rglobals[key]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ llm = [
"litellm",
"pillow",
"pydantic",
"restrictedpython>=8.1"
]
prettyprinter = ["prettyprinter"]
docs = [
Expand Down
Loading
Loading