Why isn't pinecone returning details about the newly created namespace?

    from pinecone import Pinecone, ServerlessSpec 

    api_key_pinecone = os.getenv("PINECONE_API_KEY")
    pc = Pinecone(api_key=api_key_pinecone)

    @st.dialog("Create Namespace", width="large")
    def create_namespace():
        try:
            with st.form(key="create_namespace_key"):
                namespace_name = st.text_input("Namespace Name")
                form_submitted = st.form_submit_button(label="Submit")
                if form_submitted:
                    # Check if the index exists
                    existing_indexes = list_existing_indexes()

                    if not any(index.name == st.session_state.index_name for index in existing_indexes):
                        print("Creating new index")
                        # Create a new index if it doesn't already exist
                        pc.create_index(
                            name=st.session_state.index_name,
                            dimension=3072,
                            metric="cosine",
                            spec=ServerlessSpec(cloud="aws", region="us-east-1"),
                        )

                    # Create a new namespace
                    index = pc.Index(st.session_state.index_name)
                    # Upsert a dummy vector to create the namespace
                    index.upsert(
                        vectors=[
                            {"id": "dummy", "values": [0.1] * 3072}
                        ],
                        namespace=namespace_name,
                    )
                    index = pc.describe_index(st.session_state.index_name)
                    described_index = pc.Index(host=index.host)
                    index_stats = described_index.describe_index_stats()
                    print("index_stats :",index_stats) // namespaces dictionary is empty, why ?
                    st.rerun()
        except Exception as e:
            st.error(f"An error occurred: {str(e)}")

st.sidebar.button("Create Namespace",key=uuid.uuid4(),on_click=create_namespace)

hello everyone, I’m working on a PDF RAG app .
User’s vector data is stored in his own index ( 1 index per user )
I want a feature that my app user be able to create a document collection . this can be done using pinecone namespace .

I’m trying to create one , but I’m running into an issue.

You can see that I did call index.upsert with namespace field , but after that in the next lines when I do describe_index_stats() , I don’t get the newly created namespace name , why ?

When I do a hard refresh ( F5 on keyboard or by clicking the refresh button of browser ) then I’m able to see the newly created namespace

namespaces are created automatically during upsert operations12. When you upsert data with a namespace specified, if that namespace doesn’t exist, it is created implicitly.

For your specific issue, there could be a timing aspect to consider. When creating a new index, you should wait for the index to be fully ready before performing operations. The same principle may apply to namespace creation - there could be a brief delay before the namespace appears in index statistics.

Additionally, per the documentation, namespaces are designed to partition records within an index. The recommended approach for multitenancy is to:

  1. Use one namespace per customer
  2. Target each customer’s writes and queries to their dedicated namespace

This approach offers benefits like:

  • Faster queries since every query targets one namespace
  • Data isolation between customers

We recommend adding a small delay or check after the upsert operation before calling describe_index_stats() to ensure the namespace has been fully created and is visible in the stats.

For your use case of document collections, using namespaces is indeed the recommended approach since they are specifically designed to divide records within an index into separate groups

1 Like