Description
Context:
The recent integration of the Symfony Serializer has standardized object normalization within the library. However, the current design presents challenges for developers aiming to extend message functionalities with business-specific metadata, such as timestamps for session continuity.
Identified Challenges:
- Final Message Class:
The*Message
class is declared asfinal
, preventing inheritance and thus limiting the ability to introduce custom properties or behaviors tailored to specific business needs.
final readonly class SystemMessage implements MessageInterface
Similar final declarations exist for UserMessage
, AssistantMessage
, and ToolCallMessage
. Because these classes cannot be extended, it is difficult to attach additional state (timestamp example) while still leveraging the library’s serializer.
- Serializer Accessibility:
The serializer instance is currently private and inlined within thePlatform
class, restricting the injection of custom normalizers or encoders necessary for handling extended message structures.
public function __construct(
iterable $modelClients,
iterable $responseConverter,
private ?Contract $contract = null,
) {
$this->contract = $contract ?? Contract::create();
public static function create(NormalizerInterface ...$normalizer): self
{
// Messages
$normalizer[] = new MessageBagNormalizer();
$normalizer[] = new AssistantMessageNormalizer();
...
return new self(
new Serializer($normalizer),
);
}
-
Rigid Normalizer Configuration:
Since there is no public API to inject additional normalizers after a Platform is constructed, customizing serialization for extended message classes requires rebuilding the entire serializer.
Adding custom normalizers requires modifications across multiple platform factories or constructors, increasing the complexity and maintenance overhead for developers seeking to customize serialization behavior.
PhpLlm\LlmChain\Platform\Platform.php
PhpLlm\LlmChain\Platform\Bridge\Anthropic\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\Azure\OpenAI\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\Bedrock\Platform.php // sidenote: no factory?
PhpLlm\LlmChain\Platform\Bridge\Google\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\HuggingFace\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\Mistral\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\OpenRouter\PlatformFactory.php
PhpLlm\LlmChain\Platform\Bridge\Replicate\PlatformFactory.php
Proposed Enhancements:
-
Make the Message Classes Extensible:
Remove thefinal
keyword from the*Message
specific classes to allow developers to subclass and introduce additional properties, such as timestamps or other business specific context data, facilitating richer context management. -
Expose Platform Serializer for decoration:
Expose the constructed serializers so that user-land code can decorate and use their own extra normalizers if needed. -
Optional: Streamline Normalizer Integration:
Introduce a more flexible system for registering custom normalizers, possibly through service tagging or configuration options, to simplify the process of extending serialization logic across different platforms.
Benefits:
- Empowers developers to tailor message structures to their specific business requirements.
- Enhances the modularity and maintainability of the codebase by promoting cleaner extension points.