Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ jobs:
- name: Lint with ruff
run: make lint

# TODO: enable this once all the tests pass
# - name: Run tests
# run: make tests
- name: Run tests
run: make tests
8 changes: 2 additions & 6 deletions src/guardrails/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class GuardrailResult:
original_exception (Exception | None): The original exception if execution failed.
info (dict[str, Any]): Additional structured data about the check result,
such as error details, matched patterns, or diagnostic messages.
Must include 'checked_text' field containing the processed/validated text.
Defaults to an empty dict.
Implementations may include a 'checked_text' field containing the
processed/validated text when applicable. Defaults to an empty dict.
"""

tripwire_triggered: bool
Expand All @@ -82,9 +82,6 @@ class GuardrailResult:

def __post_init__(self) -> None:
"""Validate required fields and consistency."""
if "checked_text" not in self.info:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only main code change is this. This validation is not compatible with the current tests. If we think tests should be changed, I am happy to work on it later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line is fine

raise ValueError("GuardrailResult.info must contain 'checked_text' field")

# Ensure consistency: if execution_failed=True, original_exception should be present
if self.execution_failed and self.original_exception is None:
raise ValueError(
Expand All @@ -108,5 +105,4 @@ def __post_init__(self) -> None:
TCfg (TypeVar): The configuration type, usually a Pydantic model.
Returns:
GuardrailResult or Awaitable[GuardrailResult]: The outcome of the guardrail check.
The result must include 'checked_text' in the info dict.
"""
7 changes: 6 additions & 1 deletion tests/unit/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def check(_ctx: CtxProto, _value: str, _config: int) -> GuardrailResult:
return GuardrailResult(tripwire_triggered=False)

model = _resolve_ctx_requirements(check)
fields = getattr(model, "model_fields", getattr(model, "__fields__", {}))
# Prefer Pydantic v2 API without eagerly touching deprecated v1 attributes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to resolve warning:

tests/unit/test_registry.py::test_resolve_ctx_protocol_creates_model
  /Users/seratch/code/openai-guardrails-python/tests/unit/test_registry.py:40: PydanticDeprecatedSince20: The `__fields__` attribute is deprecated, use `model_fields` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.11/migration/
    fields = getattr(model, "model_fields", getattr(model, "__fields__", {}))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

fields = (
model.model_fields
if hasattr(model, "model_fields")
else getattr(model, "__fields__", {})
)
assert issubclass(model, BaseModel) # noqa: S101
assert set(fields) == {"foo"} # noqa: S101

Expand Down
Loading