Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of datatypes or Nested Models Not Displaying Correctly in pydantic_input form #74

Open
3 of 4 tasks
gaurav-brandscapes opened this issue Feb 21, 2025 · 0 comments
Open
3 of 4 tasks
Labels
status:needs-triage Has not been triaged yet type:bug Something isn't working

Comments

@gaurav-brandscapes
Copy link

gaurav-brandscapes commented Feb 21, 2025

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

Description:

When using pydantic_input with lists (e.g., List[str]) or list of models, the form do not render correctly.
This issue occurs despite following the correct usage patterns from the [official examples], specifically the Complex Instance Model example. This behavior is observed for:

  • Simple lists like List[str], List[int]
  • List of Pydantic models
# Nested Pydantic Model
class NestedModel(BaseModel):
    id: int = Field(..., description="ID of the nested object")
    name: str = Field(..., description="Name of the nested object")

# Main ConfigModel
class ConfigModel(BaseModel):
    keys: List[str] = Field(..., description="List of keys used for lookup")
    value: str = Field(..., description="Value associated with the keys")
    nested_items: List[NestedModel] = Field(..., description="List of nested model instances")

The above models give the following form when used with pydantic_input method

Image

Expected Behaviour
Image

Reproducible Code Example

import streamlit as st
from pydantic import BaseModel, Field
from typing import List, Dict, Any
import streamlit_pydantic as sp

# Nested Pydantic Model
class NestedModel(BaseModel):
    id: int = Field(..., description="ID of the nested object")
    name: str = Field(..., description="Name of the nested object")

# Main ConfigModel
class ConfigModel(BaseModel):
    keys: List[str] = Field(..., description="List of keys used for lookup")
    value: str = Field(..., description="Value associated with the keys")
    nested_items: List[NestedModel] = Field(..., description="List of nested model instances")


# -------------------------
# Streamlit UI
# -------------------------
st.set_page_config(layout="wide")
st.title("ConfigModel Input Form")

# Pre-filled data (Optional)
data = {
    "keys": ["id1", "id2"],
    "value": "example_value",
    "metadata": [{"key": "meta1", "value": 100}, {"key": "meta2", "value": 200}],
    "nested_items": [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]
}

model_instance = ConfigModel(**data)

# Render the form using streamlit_pydantic
with st.form("config_model_form"):
    form_result = sp.pydantic_input(key="config_model", model=model_instance)
    submit_button = st.form_submit_button("Submit")

# Handle form submission
if submit_button:
    if form_result:
        try:
            validated_data = ConfigModel.model_validate(form_result).dict()
            st.success("Form submitted successfully!")
            st.json(validated_data, expanded=True)
        except Exception as e:
            st.error(f"Validation error: {str(e)}")
    else:
        st.warning("Form submission failed. Please check the inputs.")

Steps To Reproduce

  1. Install dependencies from the provided requirements.txt.
  2. Save the code as app.py.
  3. Run the code using:
streamlit run app.py

Dependencies:

pydantic-core==2.27.2
pydantic==2.10.6
streamlit==1.42.1
streamlit-pydantic==0.6.1-rc.2
pydantic-extra-types==2.10.2
pydantic-settings==2.7.1

Expected Behavior

  • The keys field should display as a list input with pre-filled values "id1", "id2".
  • The nested_items field should render forms for each instance of the NestedModel within a list form.

Current Behavior

  • The list input does not appear or behaves incorrectly.
  • List of models within nested_items are not displayed as expected, and editing or adding new instances is not possible.

Image

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • OS: Ubuntu 22.04
  • Python: 3.9, 3.11
  • Streamlit: 1.42.1
  • Pydantic: 2.10.6
  • streamlit-pydantic: 0.6.1-rc.2, 0.6.1-rc.3

Additional Information

Please advise on whether this is a compatibility issue or requires additional configuration. 😊

@gaurav-brandscapes gaurav-brandscapes added status:needs-triage Has not been triaged yet type:bug Something isn't working labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:needs-triage Has not been triaged yet type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant