Skip to content
Merged
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
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
# auth, retries, or model schemas — see the substrate pin in
# .upstreamer/upstreamer.md. Upper bound because a 2.x could move the
# Responses API surface this port binds to.
"jsonschema>=4.23,<5",
"openrouter>=1.1,<2",
"pydantic>=2.11.2",
]
Expand All @@ -53,6 +54,7 @@ dev = [
"pytest-asyncio>=0.23,<1.0",
"pytest-cov>=5.0,<8.0",
"ruff>=0.14",
"types-jsonschema>=4.23,<5",
]

[tool.hatch.build.targets.wheel]
Expand Down
13 changes: 12 additions & 1 deletion src/openrouter_agent/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import AsyncIterable, Iterable, Mapping
from typing import Any, Callable, Dict, List

from jsonschema import validators as jsonschema_validators
from pydantic import BaseModel, TypeAdapter


Expand Down Expand Up @@ -127,16 +128,26 @@ def schema_to_json_schema(schema: Any) -> Dict[str, Any]:
return {"type": "object", "properties": {}}


def _validate_json_schema(schema: Dict[str, Any], value: Any) -> Any:
cleaned = sanitize_json_schema(schema)
validator_cls = jsonschema_validators.validator_for(cleaned)
validator_cls.check_schema(cleaned)
validator_cls(cleaned).validate(value)
return value


def validate_schema(schema: Any, value: Any) -> Any:
if schema is None:
return value
if isinstance(schema, dict):
return _validate_json_schema(schema, value)
if isinstance(schema, type) and issubclass(schema, BaseModel):
return schema.model_validate(value)
if hasattr(schema, "validate_python"):
return schema.validate_python(value)
if hasattr(schema, "model_validate"):
return schema.model_validate(value)
if callable(schema) and not isinstance(schema, dict):
if callable(schema):
return schema(value)
return value

Expand Down
16 changes: 13 additions & 3 deletions src/openrouter_agent/tool_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,32 @@ async def apply_on_response_received_hooks(
rewritten.append(original)
continue
hook = get_tool_function(tool).get("on_response_received")
output_schema = get_tool_function(tool).get("output_schema")
ctx = build_tool_execute_context(tool, turn_context, context_store, shared_context_schema)
new_item = dict(item)
raw = json_loads_maybe(new_item.get("output"))
if not hook:
try:
validate_schema(get_tool_function(tool).get("output_schema"), raw)
if output_schema is None:
rewritten.append(original)
continue
try:
validated = validate_schema(output_schema, raw)
serialized = validated if is_content_array(validated) else json_dumps(validated)
except Exception as exc:
new_item["output"] = json_dumps({"error": str(exc), "originalOutput": raw})
changed = True
rewritten.append(new_item)
continue
if serialized == new_item.get("output"):
rewritten.append(original)
else:
new_item["output"] = serialized
changed = True
rewritten.append(new_item)
continue
try:
transformed = await maybe_await(hook(raw, ctx))
transformed = validate_schema(get_tool_function(tool).get("output_schema"), transformed)
transformed = validate_schema(output_schema, transformed)
new_item["output"] = transformed if is_content_array(transformed) else json_dumps(transformed)
except Exception as exc:
new_item["output"] = json_dumps({"error": str(exc), "originalOutput": raw})
Expand Down
Loading
Loading