Skip to content

feat: improve output rails error handling for SSE format - #1058

Merged
Pouyanpi merged 5 commits into
developfrom
feat/sse-output-rails
Mar 21, 2025
Merged

feat: improve output rails error handling for SSE format#1058
Pouyanpi merged 5 commits into
developfrom
feat/sse-output-rails

Conversation

@Pouyanpi

@Pouyanpi Pouyanpi commented Mar 21, 2025

Copy link
Copy Markdown
Collaborator

Description

  • improve output rails error handling format when using SSE streaming
  • return standard JSON error format with improved metadata for output rails violations
  • enable more informative error detection and handling by server implementations

resolves #1032

Related PRs

#1012

Test plan

see nmp#745

  • test streaming output rails with dummy check output config
  • verify the error format is compatible with OpenAI client error handling
  • test both stream_first=true and stream_first=false scenarios

TODO:

  • update docs

@Pouyanpi
Pouyanpi requested a review from tgasser-nv March 21, 2025 11:52
@Pouyanpi Pouyanpi self-assigned this Mar 21, 2025
@Pouyanpi Pouyanpi added enhancement New feature or request MS labels Mar 21, 2025
@Pouyanpi Pouyanpi added this to the v0.13.0 milestone Mar 21, 2025
@Pouyanpi

Copy link
Copy Markdown
Collaborator Author

@mikemckiernan I made the necessary change to the docs, please feel free to change it as you wish (you can open a separate MR if you prefer and I'll delete the doc commit), Thanks!

@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview

https://nvidia.github.io/NeMo-Guardrails/review/pr-1058

@Pouyanpi
Pouyanpi requested a review from mikemckiernan March 21, 2025 11:57

@tgasser-nv tgasser-nv 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.

Approved, please take a look and address the comments before shipping

Comment thread nemoguardrails/utils.py
Comment on lines +436 to +438
MAX_ERROR_MESSAGE_SIZE = 400
MAX_JSON_SIZE = 500
MAX_PARSING_DEPTH = 2

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.

Curious where these configs came from? How do we know this is sufficient to capture all errors we could get back?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

some random (reasonable) guess, like I don't expect langchain providers to generate error messages that are too nested or larger. Do you have any suggestion?

Comment thread nemoguardrails/utils.py
if len(error_message) > MAX_ERROR_MESSAGE_SIZE:
error_message = error_message[:MAX_ERROR_MESSAGE_SIZE] + "... (truncated)"

# OpenAI error format typically has "Error code: XXX - {...}" format

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 is fine for now, but we need a more formal specification of the OpenAI spec that we put in one file and test against. Ideally this would be vended by OpenAI themselves, but we can use observed behaviour from their prod LLMs and/or their Python client to explicitly modify the behavioiur

Could you add a backlog item to come back to this (maybe "Consolidate OpenAI Interface Spec"?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, I'll update it later: #1059

Comment thread nemoguardrails/utils.py

# looking for suspicious patterns
# is it ok?
if re.search(r"__[\w]+__", json_part):

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.

Could you get some help from Security to define what unsafe content is here? I'm concerned we pass this into ast.literal_eval() and could potentially run unsafe code from a man-in-the-middle type attack

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

the error_message should come from an error message that a langchain provider generates, for MS we use our own client so we should be safe, in the toolkit should we ensure this or is it on user's ? Also Github Code QL is not complaining,I think @erickgalinkin can help here.

Comment thread tests/test_streaming.py Outdated
expected_output = {
"event": "ABORT",
"data": {"reason": "Blocked by self check output rails."},
expected_error = {

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.

We have a quite a few "stringly typed" errors in the codebase. Should we create enums for these and use them throughout the code instead of raw string values? It looks like the type, param and code could all be enums. I suspect the message comes from Colang (is there a way to programmatically pull out the response message from a colang file)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The message comes from langchain via LLMException. But I'll maked it typed 👍🏻

@Pouyanpi Pouyanpi mentioned this pull request Mar 21, 2025
@Pouyanpi
Pouyanpi merged commit 607e4cc into develop Mar 21, 2025
@mikemckiernan

Copy link
Copy Markdown
Collaborator

@mikemckiernan I made the necessary change to the docs, please feel free to change it as you wish (you can open a separate MR if you prefer and I'll delete the doc commit), Thanks!

The update looks good. TY for making it and merging.

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

Labels

enhancement New feature or request MS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add proper SSE for output rails streaming

3 participants