Closed off6atomic closed 1 month ago
I believe this is the same issue as https://github.com/pydantic/pydantic/issues/9467
For now the answer is to add SkipValidation
to your history
:
from pydantic import SkipValidation
...
class Librarian(OpenAICall):
prompt_template = """
SYSTEM: You are the world's greatest librarian.
MESSAGES: {history}
"""
history: SkipValidation[list[ChatCompletionMessageParam]] = []
...
Thanks @willbakst
It solves the problem of ValidatorIterator
. But it doesn't solve the problem of missing Chat Completion section though.
Here is the minimal code that shows missing Chat Completion section:
import pprint
import logfire
from dotenv import load_dotenv
from mirascope.logfire import with_logfire
from mirascope.openai import OpenAICall
from openai.types.chat import ChatCompletionMessageParam
from pydantic import SkipValidation
load_dotenv()
logfire.configure()
@with_logfire
class Librarian(OpenAICall):
prompt_template = """
SYSTEM: You are the world's greatest librarian. You answer very concisely.
MESSAGES: {history}
"""
history: SkipValidation[ChatCompletionMessageParam] = []
history = [
{
"role": "user",
# uncomment below line to see LLM Chat Completions section
# "content": "What fantasy book should I read?",
"content": [{"type": "text", "text": "What fantasy book should I read?"}],
},
]
librarian = Librarian(history=history)
pprint.pprint(librarian.messages(), indent=2)
print()
print(librarian.call().content)
You can try to uncomment the content
field and comment the other one, and you will see the LLM Chat Completions section showing properly on Logfire.
What is the workaround to this bug?
Oh, I believe this is an issue with logfire's integration with LLMs as it doesn't handle a content array (only a single string content).
I will take a deeper look and likely post a bug on their repo if I can confirm it's a bug without Mirascope :)
If they are buggy like that then maybe it's a not a good idea for me to use them. Because I'll also have to show text along with images too. Do you have alternatives which you like?
I used LangSmith with LangChain in the past and it was working fine but it's not allowing arbitrary log like Logfire.
Maybe LangFuse is a good alternative?
I don’t believe LangFuse allows for arbitrary logging? I would have to check.
From my understanding if Logfire, the bug should be a simple UI fix to properly render the content. All of the content is still there it’s just not handled properly.
OK. I'll wait for them to fix the bug then. Thank you for helping me reporting bug on their repo. I appreciate it very much.
@willbakst Is this bug already reported on upstream repo? I just want to keep track of it.
@off6atomic thank you for the reminder I totally blanked on posting this. It's posted now.
I am closing this issue as it is entirely external.
For the Pydantic ValidationIterator
issue, the stop-gap measure is to use SkipValidation
, which prevents iterators from getting converted into ValidationIterator
instances (since they won't be validated).
For the logfire UI issues, we can continue to track it in the issue I posted on their repo (see previous comment).
Description
I ran the following code:
And got this message:
Notice the
ValidatorIterator
stuff. It should not be there, is it?I strongly believe that this is the culprit that causes logging with Logfire to not show Chat Completion section.
By Chat Completion section, I mean the pretty log like in the following image:
Debugging tips
content
field to be a string instead of a list of dicts, then there would be no validation error.with_logfire
decorator to wrap theLibrarian
class to see Chat Completion section disappearing when the content is a list of dictsPython, Mirascope & OS Versions, related packages (not required)