Skip to content

feat: guardrails endpoint#939

Open
Prajna1999 wants to merge 8 commits into
mainfrom
feature/guardrails-standalone-api
Open

feat: guardrails endpoint#939
Prajna1999 wants to merge 8 commits into
mainfrom
feature/guardrails-standalone-api

Conversation

@Prajna1999

Copy link
Copy Markdown
Collaborator

Summary

Target issue is #PLEASE_TYPE_ISSUE_NUMBER
Explain the motivation for making this change. What existing problem does the pull request solve?

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

Please add here if any other information is required for the reviewer.

@Prajna1999 Prajna1999 self-assigned this Jun 16, 2026
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (1)
  • ready-for-review

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: CHILL

Plan: Pro

Run ID: f09c1299-18d7-4b45-a1e6-4f4ff1382586

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
  • Commit unit tests in branch feature/guardrails-standalone-api

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 and usage tips.

@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown

OpenAPI changes   🟢 2 non-breaking changes

Tip

Safe to merge from an API-contract perspective.

Full changelog  ·  2
Method Path Change
🟢 POST /api/v1/guardrails endpoint added
🟢 GET /api/v1/guardrails/{job_id} endpoint added

mainb26f6d14 · generated by oasdiff

@Ayush8923 Ayush8923 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Added few comments.

# response_id is server-minted so callers always have a stable
# correlation handle, independent of whether the upstream guardrails
# service returns one.
response_id = str(uuid4())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This uuid4 is undefined. need to define above from uuid import UUID, uuid4

Comment thread backend/app/services/guardrails/jobs.py Outdated
Comment thread backend/app/services/guardrails/jobs.py Outdated
Comment on lines +50 to +62
job = job_crud.create(
job_type=JobType.LLM_GUARDRAILS,
trace_id=trace_id,
project_id=project_id,
)
span.set_attribute("guardrails.job_id", str(job.id))

# Persist the inbound request for traceability before the worker runs.
# We intentionally store the raw text: this endpoint exists to inspect
# potentially-unsafe content, so the request body itself is the audit log.
job_crud.update(
job_id=job.id,
job_update=JobUpdate(meta={"request": request.model_dump(mode="json")}),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Right now, we are doing 2 commits here, like:

INSERT --> COMMIT

// Then again
UPDATE --> COMMIT

which is unnecessary because the row was just created.

Since JobCrud.create() now supports meta, pass it during creation itself:

job = job_crud.create(
    job_type=JobType.LLM_GUARDRAILS,
    trace_id=trace_id,
    project_id=project_id,
    meta={
        "request": request.model_dump(mode="json")
    },
)

Then we don't need this:

job_crud.update(
    job_id=job.id,
    job_update=JobUpdate(
        meta={"request": request.model_dump(mode="json")}
    ),
)

The final code looks like this:

job = job_crud.create(
    job_type=JobType.LLM_GUARDRAILS,
    trace_id=trace_id,
    project_id=project_id,
    meta={
        "request": request.model_dump(mode="json")
    },
)

span.set_attribute("guardrails.job_id", str(job.id))

Comment on lines +97 to +98
@celery_app.task(bind=True, queue="high_priority", priority=9)
@gevent_timeout(settings.CELERY_TASK_SOFT_TIME_LIMIT, "run_guardrails_job")

@Ayush8923 Ayush8923 Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One thing: currently the traffic volume is expected to be maybe low from TAP, but I agree that guardrails jobs could eventually compete with interactive LLM requests. Let’s track this as a follow-up and evaluate a dedicated queue/lower priority once usage increases.

Comment thread backend/app/services/guardrails/jobs.py Outdated

warnings: list[str] = []
if outcome.bypassed:
warnings.append("guardrails_service_unavailable_text_returned_unchanged")

@Ayush8923 Ayush8923 Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For these types of warning crate the one constant: like:

GUARDRAILS_SERVICE_UNAVAILABLE = (
    "guardrails_service_unavailable_text_returned_unchanged"
)

and then use it here for better consistency.

Comment thread backend/app/services/guardrails/jobs.py Outdated
# being unreachable during the config-fetch step (it swallows
# transport errors and returns []). Surface this as a warning so
# callers do not mistake an effective no-op for a clean pass.
warnings.append("validator_configs_unresolved_text_returned_unchanged")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same for this, check this comment.

Comment thread backend/app/api/routes/guardrails.py Outdated
warnings = [w for w in raw_warnings if isinstance(w, str)]

guardrails_response: GuardrailsCallbackData | None = None
if job.status.value == JobStatus.SUCCESS:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we do like this:

if job.status == JobStatus.SUCCESS:

Comment thread backend/app/api/routes/guardrails.py Outdated
Ayush8923 and others added 4 commits June 16, 2026 12:59
@Prajna1999 Prajna1999 marked this pull request as ready for review June 18, 2026 03:00
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