Repository: truera/trulens
Issue: [BUG]getting error when importing trulens
Root cause: External Dependency Breakage

**Bug Description**
TypeError: Type parameter +R without a default follows type parameter with a default

**To Reproduce**
Which steps should someone take to run into the same error? A small, reproducible code example is useful here.
![image](https://github.com/user-attachments/assets/ab401e8b-9b17-4232-94a9-92987726a592)

**Expected behavior**
A clear and concise description of what you expected to happen.

**Relevant Logs/Tracebacks**
Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks. If the issue is related to the TruLens dashboard, please also include a screenshot.
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:\Py_api_test\trulens-test\main.py", line 3, in <module>
    from trulens_eval import Feedback, TruLlama
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\__init__.py", line 20, in <module>
    from trulens_eval import tru as mod_tru
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\tru.py", line 28, in <module>
    from trulens_eval.database import sqlalchemy
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\database\sqlalchemy.py", line 23, in <module>
    from trulens_eval import app as mod_app
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\app.py", line 22, in <module>
    from trulens_eval import feedback as mod_feedback
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\__init__.py", line 3, in <module>
    from trulens_eval.feedback import feedback as mod_feedback
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\feedback.py", line 25, in <module>
    from trulens_eval.feedback.provider import base as mod_base_provider
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\provider\__init__.py", line 1, in <module>
    from trulens_eval.feedback.provider.base import Provider
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\provider\base.py", line 10, in <module>
    from trulens_eval.feedback import prompts
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\prompts.py", line 6, in <module>
    from trulens_eval.feedback.v2 import feedback as v2
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\trulens_eval\feedback\v2\feedback.py", line 4, in <module>
    from langchain.evaluation.criteria.eval_chain import _SUPPORTED_CRITERIA
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langchain\evaluation\__init__.py", line 56, in <module>
    from langchain.evaluation.agents import TrajectoryEvalChain
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langchain\evaluation\agents\__init__.py", line 3, in <module>
    from langchain.evaluation.agents.trajectory_eval_chain import TrajectoryEvalChain
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langchain\evaluation\agents\trajectory_eval_chain.py", line 22, in <module>
    from langchain_core.callbacks.manager import (
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langchain_core\callbacks\__init__.py", line 23, in <module>
    from langchain_core.callbacks.manager import (
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langchain_core\callbacks\manager.py", line 29, in <module>
    from langsmith.run_helpers import get_run_tree_context
  File "C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\langsmith\run_helpers.py", line 197, in <module>
    class SupportsLangsmithExtra(Protocol, Generic[P, R]):
  File "C:\Users\alisa\AppData\Local\Programs\Python\Python310\lib\typing.py", line 312, in inner
    return func(*args, **kwds)
  File "C:\Users\alisa\AppData\Local\Programs\Python\Python310\lib\typing.py", line 1346, in __class_getitem__
    return _GenericAlias(cls, params,
  File "C:\Users\alisa\AppData\Local\Programs\Python\Python310\lib\typing.py", line 1025, in __init__
    self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
  File "E:\Py_api_test\trulens-test\.venv\lib\site-packages\pip\_vendor\typing_extensions.py", line 3019, in _collect_type_vars
    raise TypeError(f'Type parameter {t!r} without a default'
TypeError: Type parameter +R without a default follows type parameter with a default
**Environment:**
 - OS:  Windows
 - Python Version: 3.10
 - TruLens version: 0.33.0
 - Versions of other relevant installed libraries

**Additional context**
Add any other context about the problem here.


<!-- Greeting -->
Hello, @Lorrrrraine! I'm here to help you with any bugs, questions, or contributions.

<!-- Answer -->
To resolve the `TypeError: Type parameter +R without a default follows type parameter with a default` when importing the `trulens_eval` library, you need to ensure that the type parameters in your code are ordered correctly. Specifically, type parameters without defaults should precede those with defaults.

This error typically arises in generic class definitions or function annotations. Here is a general example of how to correct such an error:

```python
from typing import TypeVar, Generic

# Incorrect order of type parameters
T = TypeVar('T', bound=int)
R = TypeVar('R', default=str)

class Example(Generic[T, R]):  # This will raise the TypeError
    pass
```

To fix this, ensure that type parameters without defaults come before those with defaults:

```python
from typing import TypeVar, Generic

# Correct order of type parameters
T = TypeVar('T', bound=int)
R = TypeVar('R', default=str)

class Example(Generic[T, R]):  # This is correct
    pass
```

In the `trulens_eval` library, there are specific type parameter definitions that might be causing the `TypeError`. For instance, the `TruLlama` class has type parameters defined for its `app` attribute and `root_callable` attribute:

```python
app: Union[BaseQueryEngine, BaseChatEngine]

root_callable: ClassVar[FunctionOrMethod] = Field(
    default_factory=lambda: FunctionOrMethod.of_callable(TruLlama.query)
)
```

Additionally, the `constructor_of_class` function raises a `TypeError` if an unknown component type is encountered:

```python
def constructor_of_class(cls: Class) -> Type[app.TrulensComponent]:
    for view in COMPONENT_VIEWS:
        if view.class_is(cls):
            return view

    raise TypeError(f"Unknown trulens component type with class {cls}")
```

Moreover, there is a known issue involving the use of `BaseLLM` and `BaseChatModel` within pydantic v2, which could be causing the type parameter error:

```python
# Cannot validate BaseLLM / BaseChatModel as they are pydantic v1 and there
# is some bug involving their use within pydantic v2.
# https://github.com/langchain-ai/langchain/issues/10112
```

Ensure that your type parameters are ordered correctly and check for any known issues with the libraries you are using <sup>[[1]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/tru_llama.py)</sup><sup>[[2]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/utils/trulens.py)</sup><sup>[[3]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/utils/python.py)</sup><sup>[[4]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/feedback/provider/endpoint/langchain.py)</sup>.


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*


Same bug here when trying to run quickstart.py in VSCode.

FIXED:

In my virtual environment on VSCode, when initializing the venv it had a different version as opposed to the one I was using. I was using 3.11.8 and it was creating venv of 3.9.3.

I was able to avoid this error by installing 
```
langchain==0.2.10
langchain-core==0.2.22
```

@Lorrrrraine, what version of langchain do you have installed?

> @Lorrrrraine, what version of langchain do you have installed?

langchain==0.2.11
langchain-core==0.2.24

> I was able to avoid this error by installing
> 
> ```
> langchain==0.2.10
> langchain-core==0.2.22
> ```
Thanks, unfortunately, it hasn't worked out for my situation.


I was able to resolve this error by downgrading pip
```
pip == 24.1.2
```

Appears to be related to Langchain's typing of `SupportsLangsmithExtra(Protocol, Generic[P, R])`. I gave @rishwanth-thiyagaraj's suggestion a try and reinstalled Langchain which seemed to have fixed this error.

```
pip install pip==24.1.2
pip install -U langchain
```

@Lorrrrraine can you confirm if that helps here?

> Appears to be related to Langchain's typing of `SupportsLangsmithExtra(Protocol, Generic[P, R])`. I gave @rishwanth-thiyagaraj's suggestion a try and reinstalled Langchain which seemed to have fixed this error.
> 
> ```
> pip install pip==24.1.2
> pip install -U langchain
> ```
> 
> @Lorrrrraine can you confirm if that helps here?

Thanks for your help. But other problems have occured   :( sad
![image](https://github.com/user-attachments/assets/27414727-2094-4991-9a8d-65c45f0de66f)


> Appears to be related to Langchain's typing of `SupportsLangsmithExtra(Protocol, Generic[P, R])`. I gave @rishwanth-thiyagaraj's suggestion a try and reinstalled Langchain which seemed to have fixed this error.
> 
> ```
> pip install pip==24.1.2
> pip install -U langchain
> ```
> 
> @Lorrrrraine can you confirm if that helps here?

I was getting this error while running TruVirtual app and this has worked for me.  Thx

> Appears to be related to Langchain's typing of `SupportsLangsmithExtra(Protocol, Generic[P, R])`. I gave @rishwanth-thiyagaraj's suggestion a try and reinstalled Langchain which seemed to have fixed this error.
> 
> ```
> pip install pip==24.1.2
> pip install -U langchain
> ```
> 
> @Lorrrrraine can you confirm if that helps here?

I was having this issue too so I created simplified docker image with my same base setup with Ubuntu 22.04.4 LTS, Python 3.10.12, pip 24.2 and the following in my requirements.txt:
```
openai
trulens-eval
chromadb
notebook
ipywidgets 
jupyter
pipdeptree
```

For me downgrading from pip 24.2 to 24.1.2 alone did not fix the issue.

Since I am using docker it was easy to restart from scratch (e.g. `docker-compose up --build --force-recreate`) to see exactly which change resolved the issue.

**Downgrading from pip 24.2 to 24.1.2 and then doing an upgrade on langchain resolved this issue for me.**

It is strange though because `pip install -U langchain` results in `langchain==0.2.12` before and after running the upgrade. 

And no dependencies change. All requirements were already satisfied when the upgrade ran and I ran `pipdeptree -p langchain` before and after the upgrade and then did a diff of the output and it was identical (no  installed dependencies that langchain uses were changed). 

Since it is in a docker container I rebuilt it and tried just doing the `pip install -U langchain` and that would not fix the issue. Also tried just downgrading pip to 24.1.2 and that didn't fix the issue. But even if I then did the downgrade of pip to 24.1.2 after the `pip install -U langchain` then it would resolve the issue. Strange that the order of downgrading pip and running the upgrade of langchain did not make a difference and either way would resolve the issue.

It isn't clear to me what is causing this strange behavior.

@rishwanth-thiyagaraj  @sfc-gh-chu Any idea why this fixes the issue?

I tried recreating my docker image with pip==24.1.2 from the start instead of letting it default to pip 24.2 and have it do a fresh install of all the packages and this issue did not occur. So maybe this is an issue with pip 24.2 and langchain?

I've also faced this error and finally fixed the error by changing the order of import statements. The Langchain imports must be first, before the Trulens Tru import.

for example, this causes the failure discussed above:
```
from trulens_eval import Tru
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
...
```

To fix:
```
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
...
from trulens_eval import Tru
```

Hey all - thanks for your patience.

We've fixed this issue in 0.33.1, 0.32.1, and 0.31.1 so you can now use TruLens-Eval with the latest pip version.

`pip install -U trulens_eval && pip install -U pip
`