Skip to content

LCORE-3201: Expand Shield Interface for direct running#2220

Draft
Jazzcort wants to merge 2 commits into
lightspeed-core:feature/upgrade-ogx-to-1.0.2from
Jazzcort:add-abstract-safety-capability
Draft

LCORE-3201: Expand Shield Interface for direct running#2220
Jazzcort wants to merge 2 commits into
lightspeed-core:feature/upgrade-ogx-to-1.0.2from
Jazzcort:add-abstract-safety-capability

Conversation

@Jazzcort

Copy link
Copy Markdown
Contributor

Description

Introduce simplified V2 shield moderation models (ShieldModerationPassedV2, ShieldModerationBlockedV2, ShieldModerationResultV2) alongside the existing types to avoid breaking the responses pipeline. Add AbstractSafetyCapability in capabilities/base.py, consolidating the old safety module interface with pydantic-ai's AbstractCapability. Migrate QuestionValidity and PiiRedactionCapability to use V2 types and the new base class.

Follow-up PR will clean up the old moderation logic and rename V2 to the standard names.

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement
  • Benchmarks improvement

Tools used to create PR

Identify any AI code assistants used in this PR (for transparency and review context)

  • Assisted-by: (e.g., Claude, CodeRabbit, Ollama, etc., N/A if not used)
  • Generated by: (e.g., tool name and version; N/A if not used)

Related Tickets & Documents

  • Related Issue #
  • Closes #

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

  • Please provide detailed steps to perform tests related to this code change.
  • How were the fix/results from this change verified? Please provide relevant screenshots or results.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b44bba00-8148-4870-9c05-06519598c474

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jazzcort

Jazzcort commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Need to add a few unit tests for this. Will do it later

@Jazzcort Jazzcort changed the title Add AbstractSafetyCapability base class with V2 moderation types LCORE-3201: Expand Shield Interface for direct running Jul 23, 2026

@dataclass
class QuestionValidity(AbstractCapability[None]):
class QuestionValidity(AbstractSafetyCapability[Any]):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

object is the most suitable typevar here

Comment thread src/models/common/moderation.py Outdated
]


class ShieldModerationPassedV2(BaseModel):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can reuse old model instead of creating new ones.
You can generate moderation_id like: f"modr-{uuid.uuid4()}", it will be later useful when integrating with openai responses and conversations APIs. You can set refusal_response to optional for now.

Eliminates redundant stored state by deriving refusal_response from
message at access time, removing the now-unnecessary
create_refusal_response helper and all constructor-site arguments.
@Jazzcort
Jazzcort force-pushed the add-abstract-safety-capability branch 2 times, most recently from a5980fd to 71f4641 Compare July 23, 2026 20:29
Introduce AbstractSafetyCapability(AbstractCapability[T]) in
capabilities/base.py, requiring subclasses to implement a run()
method that accepts raw text and returns ShieldModerationResult.
This enables safety capabilities to be invoked outside the
pydantic-ai agent lifecycle.

Migrate QuestionValidity and PiiRedactionCapability to extend the
new base class and implement run():

- QuestionValidity.run() delegates to model_request and maps
  ALLOWED/REJECTED to ShieldModerationPassed/Blocked
- PiiRedactionCapability.run() applies regex redaction and returns
  Blocked when PII is detected

Add unit tests for both run() implementations.
@Jazzcort
Jazzcort force-pushed the add-abstract-safety-capability branch from 71f4641 to 6c7bac7 Compare July 23, 2026 20:31

@asimurka asimurka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

moderation_id: str
refusal_response: ResponseMessage

@property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perfect, this is even better than I suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants