Skip to content

Conversation

DePasqualeOrg
Copy link
Contributor

The silent error made debugging more difficult.

// but that is not public so just fall back to text
// It might be TokenizerError.chatTemplate("No chat template was specified"),
// but that is not public, so just fall back to text
print("Error while applying chat template: \(error.localizedDescription)")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This catch was added for the case where the tokenizer didn't have a chat template at all and to give it something to fall back on. At the time TokenizerError wasn't public so we couldn't catch it, but now it is and we want this catch to only catch this case:

        guard let selectedChatTemplate else {
            throw TokenizerError.chatTemplate("This tokenizer does not have a chat template, and no template was passed.")
        }

What do you think about doing this?

        } catch TokenizerError.chatTemplate(let message) {
            if message == "This tokenizer does not have a chat template, and no template was passed." {
                // fallback case
                let prompt =
                    messages
                    .compactMap { $0["content"] as? String }
                    .joined(separator: ". ")
                let promptTokens = tokenizer.encode(text: prompt)
                return LMInput(tokens: MLXArray(promptTokens))
            } else {
                throw TokenizerError.chatTemplate(message)
            }
        }

That way real errors get propagated as error and this fixes #150

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, although it would probably be better to have a dedicated error case for this type of error in swift-transformers and check for that case rather than the exact error message. I'll open a PR for that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, a dedicated error case would be ideal.

@DePasqualeOrg DePasqualeOrg force-pushed the log-chat-template-error branch from f52211e to f068b4a Compare April 30, 2025 10:19
@DePasqualeOrg DePasqualeOrg changed the title Log chat template error Handle missing chat template and propagate other errors Apr 30, 2025
@DePasqualeOrg DePasqualeOrg force-pushed the log-chat-template-error branch from f068b4a to 4e62ca4 Compare April 30, 2025 10:22
@DePasqualeOrg
Copy link
Contributor Author

This is ready for review.

Copy link
Collaborator

@davidkoski davidkoski left a comment

Choose a reason for hiding this comment

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

Looks great -- thank you for chasing down the dependencies!

@davidkoski davidkoski merged commit 4c681c5 into ml-explore:main Apr 30, 2025
3 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.

2 participants