Giter Club home page Giter Club logo

Comments (10)

cktang88 avatar cktang88 commented on June 1, 2024 3

One possible way might be to allow users to specify an optional pydantic model in the tag.
The model would need to have fields that matches the field names, and then all validation can be done in the pydantic model instead of in the XML

    <object name="bank_run" pydantic="BankRun">
        <string
            name="explanation"
            description="A paragraph about what a bank run is."
            on-fail-length="reask"
        />
        <url
            name="follow_up_url"
            description="A web URL where I can read more about bank runs."
            on-fail-valid-url="filter"
        />
    </object>

and then

from pydantic import BaseModel
class BankRun(BaseModel):
    explanation: str
    follow_up_url: str

There's many advantages to this, we can specify default values, optional fields, all the other stuff pydantic supports, and the wider python ecosystem in general since pydantic validation is very extensible, people can use this way to use the validators lib to plug into the pydantic schema too.

NOTE: this would also make using the @register_validator redundant

One other effect of this is that users won't need to know arbitrary tags, eg. like <url> or <string>, it'll just all be <field> or similar, all the data types will live in python.
This does loosely tie the RAIL format to Python which may not be preferable, but would be much more natural to work with in Python IMO.

from guardrails.

ShreyaR avatar ShreyaR commented on June 1, 2024 2

@cktang88 Nice, I think this would be super useful! I like this framing of the solution and the idea of adding field as a data type.

I do think that supporting primitives like url, code etc. is nice so that the spec isn't too reliant on Python. I plan to add support for rail in other languages in the future. However, to make the support for pydantic work, adding field as a datatype along with the existing data types makes a lot of sense.

I'll get a PR started to add this.

from guardrails.

krandiash avatar krandiash commented on June 1, 2024 1

Started a PR #35 that starts to address this issue (see details there)

from guardrails.

ShreyaR avatar ShreyaR commented on June 1, 2024

@devenbhooshan this is a pretty neat suggestion!

I'll think some more about what the best interface for integrating existing python validator library could be. One way to do this could be that for supported types (e.g. email, IP address, etc.), validators provided in the python validator lib is supported by default. Wdyt?

from guardrails.

cktang88 avatar cktang88 commented on June 1, 2024

would be cool to integrate w/ Pydantic directly https://docs.pydantic.dev/ which seems to be one of the most common Python schema validation libs

from guardrails.

oliverbj avatar oliverbj commented on June 1, 2024

Pydantic support would be awesome. +1

from guardrails.

ShreyaR avatar ShreyaR commented on June 1, 2024

@cktang88 @oliverbj could definitely add Pydantic support for schema validation. The Validator classes in Guardrails took inspiration from Pydantic's Validators.

Do you have an idea of what an integration with Pydantic's schema validation would be like within Guardrails?

from guardrails.

irgolic avatar irgolic commented on June 1, 2024

Could we generate a models.py file with code describing pydantic models from a guardrails spec?

from guardrails.

ShreyaR avatar ShreyaR commented on June 1, 2024

@irgolic I think so! I'll have to double check what the edge cases are around nested structures, but it should be doable.

That would make it pretty easy for users so that they don't have to make double effort to first write the spec and then the pydantic models.

from guardrails.

ShreyaR avatar ShreyaR commented on June 1, 2024

This was added in #35 (docs on how to use Pydantic). I'll close this issue out -- thanks for the suggestion everyone!

from guardrails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.