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

fix model validation in pydantic; #27

Merged
merged 9 commits into from
Feb 7, 2025
Merged

fix model validation in pydantic; #27

merged 9 commits into from
Feb 7, 2025

Conversation

aditya1503
Copy link
Contributor

Fix validation error in Pydantic:


---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[13], line 1
----> 1 codex_tool.query("WHat is this?")

File ~/anaconda3/envs/codex/lib/python3.10/site-packages/cleanlab_codex/codex_tool.py:107, in CodexTool.query(self, question)
     92 def query(
     93     self,
     94     question: Annotated[
   (...)
     97     ],
     98 ) -> Optional[str]:
     99     """Asks an all-knowing advisor this question in cases where it cannot be answered from the provided Context. If the answer is unavailable, this returns a fallback answer or None.
    100 
    101     Args:
   (...)
    105         The answer to the question if available. If no answer is available, this returns a fallback answer or None.
    106     """
--> 107     return self._project.query(question, fallback_answer=self._fallback_answer)[0]

File ~/anaconda3/envs/codex/lib/python3.10/site-packages/cleanlab_codex/project.py:176, in Project.query(self, question, fallback_answer, read_only)
    156 def query(
    157     self,
    158     question: str,
   (...)
    161     read_only: bool = False,
    162 ) -> tuple[Optional[str], Optional[Entry]]:
    163     """Query Codex to check if this project contains an answer to the question. Add the question to the project for SME review if it does not.
    164 
    165     Args:
   (...)
    174             If Codex is unable to answer the question, the first element will be `fallback_answer` if provided, otherwise None. The second element will be a new [`Entry`](/reference/python/types.entry#class-entry) in the Codex project.
    175     """
--> 176     return query_project(
    177         client=self._sdk_client,
    178         question=question,
    179         project_id=self.id,
    180         fallback_answer=fallback_answer,
    181         read_only=read_only,
    182     )

File ~/anaconda3/envs/codex/lib/python3.10/site-packages/cleanlab_codex/internal/project.py:35, in query_project(client, question, project_id, fallback_answer, read_only)
     32     return fallback_answer, entry
     34 if not read_only:
---> 35     created_entry = Entry.model_validate(client.projects.entries.add_question(project_id, question=question))
     36     return fallback_answer, created_entry
     38 return fallback_answer, None

File ~/anaconda3/envs/codex/lib/python3.10/site-packages/pydantic/main.py:627, in BaseModel.model_validate(cls, obj, strict, from_attributes, context)
    625 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    626 __tracebackhide__ = True
--> 627 return cls.__pydantic_validator__.validate_python(
    628     obj, strict=strict, from_attributes=from_attributes, context=context
    629 )

ValidationError: 1 validation error for Entry
  Input should be a valid dictionary or instance of Entry [type=model_type, input_value=Entry(created_at=datetime...None, frequency_count=0), input_type=Entry]
    For further information visit https://errors.pydantic.dev/2.10/v/model_type

@aditya1503 aditya1503 requested a review from axl1313 February 7, 2025 14:35
@elisno
Copy link
Member

elisno commented Feb 7, 2025

Are you sure updating Pydantic will resolve this?

I just installed it in a fresh environment and checked the version, but still ran into this validation error:

pip freeze | grep pydantic
# pydantic==2.10.6

I suspect the issue might be due to the Entry class defined here being different from the one in codex_sdx, but I’m not entirely sure.

Reference:

class Entry(_Entry): ...


@aditya1503 Any reason why you reverted your suggested .model_dump() lines?

@aditya1503
Copy link
Contributor Author

Are you sure updating Pydantic will resolve this?

I just installed it in a fresh environment and checked the version, but still ran into this validation error:

pip freeze | grep pydantic
# pydantic==2.10.6

I suspect the issue might be due to the Entry class defined here being different from the one in codex_sdx, but I’m not entirely sure.

Reference:

class Entry(_Entry): ...

@aditya1503 Any reason why you reverted your suggested .model_dump() lines?

reverted, I fixed the unit test case that was failing

Copy link
Collaborator

@axl1313 axl1313 left a comment

Choose a reason for hiding this comment

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

thanks for the fix! i pushed some additional changes to the tests that would actually have caught this issue as well. the mocking in the test cases was flawed before.

@axl1313 axl1313 merged commit b79317e into main Feb 7, 2025
8 checks passed
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.

3 participants