Skip to content

[BUG] filter_sensitive_fields output policy action throws an error #178

@alexzerntev

Description

@alexzerntev

Describe the Bug

The filter_sensitive_fields output policy action throws a 'TypeSchema' object has no attribute 'get' error when used. The policy action fails during output policy evaluation because nested Pydantic models are not converted to dictionaries.

To Reproduce

Steps to reproduce the behavior:

  1. Create a tool with sensitive: true fields in the return type
  2. Add an output policy with action: filter_sensitive_fields
  3. Run a test with user_context
  4. See error

Expected Behavior

The filter_sensitive_fields action should filter out fields marked as sensitive: true from the response when the policy condition is met.

Environment

  • MXCP Version: latest from pip
  • Python Version: 3.11+
  • Operating System: Any
  • Installation Method: pip

Configuration

# tool definition
return:
  type: object
  properties:
    salary:
      type: number
      sensitive: true

policies:
  output:
    - condition: "user.role != 'admin'"
      action: filter_sensitive_fields

Error Output

'TypeSchema' object has no attribute 'get'

Additional Context

Root Cause Analysis:
The bug is in enforcer.py:483-494. The _filter_sensitive_fields method expects a dict[str, Any] and calls type_def.get("sensitive", False):

# enforcer.py:483
def _filter_sensitive_fields(self, data: Any, type_def: dict[str, Any]) -> Any:
    if type_def.get("sensitive", False):  # <-- This line fails
        return None

But the caller at enforcer.py:378-382 passes endpoint_def["return"] which comes from service.py:255:

component_dict = component.model_dump(mode="python", exclude_unset=True)

When using mode="python", nested Pydantic models like TypeSchemaModel are NOT converted to dicts - they remain as Pydantic model instances. Pydantic models don't have a .get() method.

Fix Options:

  1. Change service.py:255 to use mode="json" which recursively converts to dicts
  2. Change enforcer.py to use attribute access (type_def.sensitive) instead of .get()
  3. Call .model_dump() on nested models before passing to _filter_sensitive_fields

Location in code:

  • mxcp.sdk.policy.enforcer during output policy evaluation
  • enforcer.py:483-494
  • service.py:255

This happens consistently when using the filter_sensitive_fields policy action.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions